feat: add password change, class unenroll, and optional notify flags
Let admins skip SMS on user, class, and invoice actions, and let users change their own password with the current one.
This commit is contained in:
@@ -6,6 +6,7 @@ const User = require('../users/userModel');
|
||||
const config = require('../../config/config');
|
||||
const AppError = require('../../utils/AppError');
|
||||
const { isBootstrapSuperAdminDisabled } = require('../../utils/superAdmin');
|
||||
const { assertPasswordStrength } = require('../../utils/passwordRules');
|
||||
|
||||
const generateTokens = (user) => {
|
||||
const payload = {
|
||||
@@ -104,8 +105,37 @@ const logout = async (userId, refreshTokenString) => {
|
||||
return true;
|
||||
};
|
||||
|
||||
const changePassword = async (userId, { currentPassword, newPassword, refreshToken: currentRefreshToken }) => {
|
||||
if (!currentPassword) {
|
||||
throw new AppError('INVALID_CURRENT_PASSWORD');
|
||||
}
|
||||
assertPasswordStrength(newPassword);
|
||||
|
||||
const user = await User.findById(userId);
|
||||
if (!user || !user.isActive || isBootstrapSuperAdminDisabled(user)) {
|
||||
throw new AppError('USER_NOT_FOUND');
|
||||
}
|
||||
|
||||
const isMatch = await bcrypt.compare(currentPassword, user.passwordHash);
|
||||
if (!isMatch) {
|
||||
throw new AppError('INVALID_CURRENT_PASSWORD');
|
||||
}
|
||||
|
||||
user.passwordHash = await bcrypt.hash(newPassword, 10);
|
||||
|
||||
if (currentRefreshToken) {
|
||||
user.refreshTokens = user.refreshTokens.filter((rt) => rt.token === currentRefreshToken);
|
||||
} else {
|
||||
user.refreshTokens = [];
|
||||
}
|
||||
|
||||
await user.save();
|
||||
return true;
|
||||
};
|
||||
|
||||
module.exports = {
|
||||
login,
|
||||
refreshToken,
|
||||
logout
|
||||
logout,
|
||||
changePassword
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user