Password updator for users / admin
This commit is contained in:
@@ -188,6 +188,77 @@ class AuthController {
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async changePassword(req, res) {
|
||||
try {
|
||||
const { currentPassword, newPassword } = req.body;
|
||||
|
||||
// Validate input
|
||||
if (!currentPassword || !newPassword) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'Current password and new password are required'
|
||||
});
|
||||
}
|
||||
|
||||
// Validate new password strength
|
||||
if (newPassword.length < 8) {
|
||||
return res.status(400).json({
|
||||
success: false,
|
||||
error: 'New password must be at least 8 characters long'
|
||||
});
|
||||
}
|
||||
|
||||
// Get user from session
|
||||
const userId = req.session.userId;
|
||||
const userEmail = req.session.userEmail;
|
||||
|
||||
if (!userId || !userEmail) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'Session expired. Please login again.'
|
||||
});
|
||||
}
|
||||
|
||||
// Fetch user from NocoDB to verify current password
|
||||
const user = await nocodbService.getUserByEmail(userEmail);
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).json({
|
||||
success: false,
|
||||
error: 'User not found'
|
||||
});
|
||||
}
|
||||
|
||||
// Verify current password
|
||||
const storedPassword = user.Password || user.password;
|
||||
if (storedPassword !== currentPassword) {
|
||||
return res.status(401).json({
|
||||
success: false,
|
||||
error: 'Current password is incorrect'
|
||||
});
|
||||
}
|
||||
|
||||
// Update password in NocoDB
|
||||
await nocodbService.updateUser(userId, {
|
||||
Password: newPassword
|
||||
});
|
||||
|
||||
console.log('Password changed successfully for user:', userEmail);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
message: 'Password changed successfully'
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('Change password error:', error);
|
||||
res.status(500).json({
|
||||
success: false,
|
||||
error: 'Failed to change password. Please try again later.'
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = new AuthController();
|
||||
Reference in New Issue
Block a user