shellphone.app/app/auth/mutations/change-password.ts

25 lines
685 B
TypeScript
Raw Normal View History

import { NotFoundError, SecurePassword, resolver } from "blitz";
2021-07-31 14:33:18 +00:00
import db from "../../../db";
import { authenticateUser } from "./login";
import { ChangePassword } from "../validations";
2021-07-31 14:33:18 +00:00
export default resolver.pipe(
resolver.zod(ChangePassword),
resolver.authorize(),
async ({ currentPassword, newPassword }, ctx) => {
const user = await db.user.findFirst({ where: { id: ctx.session.userId! } });
if (!user) throw new NotFoundError();
2021-07-31 14:33:18 +00:00
await authenticateUser(user.email, currentPassword);
2021-07-31 14:33:18 +00:00
const hashedPassword = await SecurePassword.hash(newPassword.trim());
2021-07-31 14:33:18 +00:00
await db.user.update({
where: { id: user.id },
data: { hashedPassword },
});
2021-07-31 14:33:18 +00:00
return true;
2021-08-01 12:04:04 +00:00
},
);