Add updatable profile

This commit is contained in:
2025-08-14 21:31:48 +02:00
parent 9cb405be22
commit e84c022bcf
5 changed files with 386 additions and 2 deletions
+44
View File
@@ -0,0 +1,44 @@
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 };
});