feat: add reset password service
parent
d608538c9e
commit
895ce83b5b
|
@ -0,0 +1,32 @@
|
|||
import * as Yup from "yup";
|
||||
import AppError from "../../errors/AppError";
|
||||
import ShowUserService from "./ShowUserService";
|
||||
|
||||
interface ResetPasswordServiceRequest {
|
||||
userPassword: string;
|
||||
userId: string | number;
|
||||
}
|
||||
const ResetPasswordService = async ({userPassword, userId}: ResetPasswordServiceRequest): Promise<void> => {
|
||||
try {
|
||||
const user = await ShowUserService(userId);
|
||||
const schema = Yup.object().shape({
|
||||
password: Yup.string(),
|
||||
});
|
||||
try {
|
||||
await schema.validate({ password: userPassword });
|
||||
} catch (err: any) {
|
||||
throw new AppError(err.message);
|
||||
}
|
||||
|
||||
await user.update({
|
||||
userPassword,
|
||||
});
|
||||
|
||||
await user.reload();
|
||||
} catch (error: any) {
|
||||
console.error('===> Error on ResetPasswordService.ts file: \n', error)
|
||||
throw new AppError(error.message);
|
||||
}
|
||||
};
|
||||
|
||||
export default ResetPasswordService;
|
Loading…
Reference in New Issue