45 lines
942 B
TypeScript
45 lines
942 B
TypeScript
import { hash, verify } from 'argon2';
|
|
import { useValidatedBody, z } from 'h3-zod';
|
|
import prisma from '~~/lib/prisma';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const { user } = await requireUserSession(event);
|
|
|
|
const { old, password } = await useValidatedBody(
|
|
event,
|
|
z.object({
|
|
password: z.string().min(8),
|
|
old: z.string().min(8)
|
|
})
|
|
);
|
|
|
|
const dbUser = await prisma.user.findUniqueOrThrow({
|
|
where: {
|
|
deletedAt: null,
|
|
id: user.id
|
|
},
|
|
select: {
|
|
id: true,
|
|
password: true
|
|
}
|
|
});
|
|
if (!(await verify(dbUser.password, old))) {
|
|
throw createError({ status: 404, statusText: 'Not found!' });
|
|
}
|
|
|
|
const passwordHash = await hash(password);
|
|
|
|
await prisma.user.update({
|
|
where: {
|
|
id: user.id
|
|
},
|
|
data: {
|
|
password: passwordHash
|
|
}
|
|
});
|
|
|
|
console.warn(`Successful changed password for user ${user.id}`);
|
|
|
|
return { ok: true };
|
|
});
|