chage api use token

This commit is contained in:
JakkrapartXD 2026-01-14 13:42:54 +07:00
parent 6239159099
commit d8a9909eb9
4 changed files with 17 additions and 23 deletions

View file

@ -188,7 +188,7 @@ export class AuthService {
const token = jwt.sign({ id: user.id, email: user.email }, config.jwt.secret, { expiresIn: '1h' });
// Create reset URL
const resetURL = `${process.env.FRONTEND_URL || 'http://localhost:3000'}/reset-password?id=${user.id}&token=${token}`;
const resetURL = `${process.env.FRONTEND_URL || 'http://localhost:3000'}/reset-password?token=${token}`;
// Create transporter
const transporter = nodemailer.createTransport({
@ -231,9 +231,10 @@ export class AuthService {
}
}
async resetPassword(id: number, token: string, password: string): Promise<ResetPasswordResponse> {
async resetPassword(token: string, password: string): Promise<ResetPasswordResponse> {
try {
const user = await prisma.user.findUnique({ where: { id } });
const decoded = jwt.verify(token, config.jwt.secret) as { id: number; email: string };
const user = await prisma.user.findUnique({ where: { id: decoded.id } });
if (!user) throw new UnauthorizedError('User not found');
const secret = config.jwt.secret;
@ -254,14 +255,15 @@ export class AuthService {
message: 'Password reset successfully'
};
} catch (error) {
logger.error('Failed to reset password', { id, error });
logger.error('Failed to reset password', { error });
throw error;
}
}
async changePassword(id: number, oldPassword: string, newPassword: string): Promise<ChangePasswordResponse> {
async changePassword(token: string, oldPassword: string, newPassword: string): Promise<ChangePasswordResponse> {
try {
const user = await prisma.user.findUnique({ where: { id } });
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);
@ -280,7 +282,7 @@ export class AuthService {
message: 'Password changed successfully'
};
} catch (error) {
logger.error('Failed to change password', { id, error });
logger.error('Failed to change password', { error });
throw error;
}
}