move chage password to user con
This commit is contained in:
parent
ff5b189b2f
commit
a6cddc6318
6 changed files with 85 additions and 57 deletions
|
|
@ -11,7 +11,6 @@ import {
|
|||
RegisterResponse,
|
||||
RefreshTokenResponse,
|
||||
ResetPasswordResponse,
|
||||
ChangePasswordResponse,
|
||||
ResetRequestResponse
|
||||
} from '../types/auth.types';
|
||||
import { UserResponse } from '../types/user.types';
|
||||
|
|
@ -260,33 +259,6 @@ export class AuthService {
|
|||
}
|
||||
}
|
||||
|
||||
async changePassword(token: string, oldPassword: string, newPassword: string): Promise<ChangePasswordResponse> {
|
||||
try {
|
||||
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; username: string; email: string; roleCode: string };
|
||||
const user = await prisma.user.findUnique({ where: { id: decoded.id } });
|
||||
if (!user) throw new UnauthorizedError('User not found');
|
||||
|
||||
const isPasswordValid = await bcrypt.compare(oldPassword, user.password);
|
||||
if (!isPasswordValid) throw new UnauthorizedError('Invalid password');
|
||||
|
||||
const encryptedPassword = await bcrypt.hash(newPassword, 10);
|
||||
|
||||
await prisma.user.update({
|
||||
where: { id: user.id },
|
||||
data: { password: encryptedPassword }
|
||||
});
|
||||
|
||||
logger.info('Password changed successfully', { userId: user.id });
|
||||
return {
|
||||
code: 200,
|
||||
message: 'Password changed successfully'
|
||||
};
|
||||
} catch (error) {
|
||||
logger.error('Failed to change password', { error });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate access token (JWT)
|
||||
*/
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue