N-FIN-47: add server action to delete account

This commit is contained in:
Markus Thielker 2024-03-17 19:55:17 +01:00
parent e809912ae3
commit 2de7d36138
No known key found for this signature in database

View file

@ -0,0 +1,58 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_SIGN_IN } from '@/lib/constants';
import { getUser, lucia } from '@/auth';
import { prismaClient } from '@/prisma';
import { cookies } from 'next/headers';
export default async function accountDelete(): Promise<ActionResponse> {
'use server';
const user = await getUser();
if (!user) {
return {
type: 'error',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
await prismaClient.payment.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.entity.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.category.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.session.deleteMany({
where: {
userId: user.id,
},
});
await prismaClient.user.delete({
where: {
id: user.id,
},
});
const sessionCookie = lucia.createBlankSessionCookie();
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
return {
type: 'success',
message: 'Your account was removed.',
redirect: URL_SIGN_IN,
};
}