N-FIN-79: refactor server actions to use auth0 session

This commit is contained in:
Markus Thielker 2024-04-04 00:48:36 +02:00
parent c4146a36a4
commit 12c689d1d6
No known key found for this signature in database
9 changed files with 81 additions and 85 deletions

View file

@ -1,58 +1,52 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import { URL_SIGN_IN } from '@/lib/constants';
import { getUser, lucia } from '@/auth';
import { URL_SIGN_IN, URL_SIGN_OUT } from '@/lib/constants';
import prisma from '@/prisma';
import { cookies } from 'next/headers';
import { getSession } from '@auth0/nextjs-auth0';
export default async function accountDelete(): Promise<ActionResponse> {
'use server';
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
await prisma.payment.deleteMany({
where: {
userId: user.id,
userId: user.sub,
},
});
await prisma.entity.deleteMany({
where: {
userId: user.id,
userId: user.sub,
},
});
await prisma.category.deleteMany({
where: {
userId: user.id,
userId: user.sub,
},
});
await prisma.session.deleteMany({
where: {
userId: user.id,
},
});
let requestOptions = {
method: 'DELETE',
redirect: 'follow',
} as RequestInit;
await prisma.user.delete({
where: {
id: user.id,
},
});
const sessionCookie = lucia.createBlankSessionCookie();
cookies().set(sessionCookie.name, sessionCookie.value, sessionCookie.attributes);
fetch(`https://login.auth0.com/api/v2/users/${user.sub}`, requestOptions)
.then(response => response.text())
.then(result => console.log(result))
.catch(error => console.log('error', error));
return {
type: 'success',
message: 'Your account was removed.',
redirect: URL_SIGN_IN,
redirect: URL_SIGN_OUT,
};
}

View file

@ -1,9 +1,9 @@
import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { categoryFormSchema } from '@/lib/form-schemas/categoryFormSchema';
import { getSession } from '@auth0/nextjs-auth0';
export default async function categoryCreateUpdate({
id,
@ -12,15 +12,15 @@ export default async function categoryCreateUpdate({
}: z.infer<typeof categoryFormSchema>): Promise<ActionResponse> {
'use server';
// check that user is logged in
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to create/update an category.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// create/update category
try {
@ -44,7 +44,7 @@ export default async function categoryCreateUpdate({
} else {
await prisma.category.create({
data: {
userId: user.id,
userId: user.sub,
name: name,
color: color,
},

View file

@ -1,7 +1,7 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { getSession } from '@auth0/nextjs-auth0';
export default async function categoryDelete(id: number): Promise<ActionResponse> {
'use server';
@ -14,21 +14,21 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
};
}
// check that user is logged in
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to delete an category.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// check that category is associated with user
const category = await prisma.category.findFirst({
where: {
id: id,
userId: user.id,
userId: user.sub,
},
});
if (!category) {
@ -43,7 +43,7 @@ export default async function categoryDelete(id: number): Promise<ActionResponse
await prisma.category.delete({
where: {
id: category.id,
userId: user.id,
userId: user.sub,
},
},
);

View file

@ -2,8 +2,8 @@ import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import { entityFormSchema } from '@/lib/form-schemas/entityFormSchema';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { getSession } from '@auth0/nextjs-auth0';
export default async function entityCreateUpdate({
id,
@ -13,15 +13,15 @@ export default async function entityCreateUpdate({
}: z.infer<typeof entityFormSchema>): Promise<ActionResponse> {
'use server';
// check that user is logged in
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to create/update an entity.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// create/update entity
try {
@ -46,7 +46,7 @@ export default async function entityCreateUpdate({
} else {
await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: name,
type: type,
defaultCategoryId: defaultCategoryId ?? null,

View file

@ -1,7 +1,7 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { getSession } from '@auth0/nextjs-auth0';
export default async function entityDelete(id: number): Promise<ActionResponse> {
'use server';
@ -14,21 +14,21 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
};
}
// check that user is logged in
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to delete an entity.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// check that entity is associated with user
const entity = await prisma.entity.findFirst({
where: {
id: id,
userId: user.id,
userId: user.sub,
},
});
if (!entity) {
@ -43,7 +43,7 @@ export default async function entityDelete(id: number): Promise<ActionResponse>
await prisma.entity.delete({
where: {
id: entity.id,
userId: user.id,
userId: user.sub,
},
},
);

View file

@ -1,32 +1,32 @@
import prisma from '@/prisma';
import type { Category, Entity } from '@prisma/client';
import { EntityType } from '@prisma/client';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { ActionResponse } from '@/lib/types/actionResponse';
import { getSession } from '@auth0/nextjs-auth0';
export default async function generateSampleData(): Promise<ActionResponse> {
'use server';
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to create/update an category.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// Categories: create sample data
const categories: Category[] = await prisma.category.findMany({where: {userId: user.id}});
if (await prisma.category.count({where: {userId: user.id}}) == 0) {
const categories: Category[] = await prisma.category.findMany({where: {userId: user.sub}});
if (await prisma.category.count({where: {userId: user.sub}}) == 0) {
console.log('Creating sample categories...');
categories.push(await prisma.category.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Groceries',
color: '#FFBEAC',
},
@ -34,7 +34,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
categories.push(await prisma.category.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Drugstore items',
color: '#9CBCFF',
},
@ -42,7 +42,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
categories.push(await prisma.category.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Going out',
color: '#F1ADFF',
},
@ -50,7 +50,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
categories.push(await prisma.category.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Random stuff',
color: '#C1FFA9',
},
@ -58,7 +58,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
categories.push(await prisma.category.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Salary',
color: '#FFF787',
},
@ -69,14 +69,14 @@ export default async function generateSampleData(): Promise<ActionResponse> {
console.log(categories);
// Entities: create sample data
const entities: Entity[] = await prisma.entity.findMany({where: {userId: user.id}});
if (await prisma.entity.count({where: {userId: user.id}}) == 0) {
const entities: Entity[] = await prisma.entity.findMany({where: {userId: user.sub}});
if (await prisma.entity.count({where: {userId: user.sub}}) == 0) {
console.log('Creating sample entities...');
entities.push(await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Main Account',
type: EntityType.Account,
},
@ -84,7 +84,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
entities.push(await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Company',
type: EntityType.Entity,
},
@ -92,7 +92,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
entities.push(await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Supermarket 1',
type: EntityType.Entity,
},
@ -100,7 +100,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
entities.push(await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Supermarket 2',
type: EntityType.Entity,
},
@ -108,7 +108,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
entities.push(await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Supermarket 3',
type: EntityType.Entity,
},
@ -116,7 +116,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
entities.push(await prisma.entity.create({
data: {
userId: user.id,
userId: user.sub,
name: 'Supermarket 4',
type: EntityType.Entity,
},
@ -129,7 +129,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
// Payments: create sample data
console.log('Creating sample payments...');
if (await prisma.payment.count({where: {userId: user.id}}) == 0) {
if (await prisma.payment.count({where: {userId: user.sub}}) == 0) {
for (let i = 0; i < 4; i++) {
const date = new Date();
@ -138,7 +138,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
await prisma.payment.create({
data: {
userId: user.id,
userId: user.sub,
amount: 200000,
date: date,
payorId: entities[1].id,
@ -166,7 +166,7 @@ export default async function generateSampleData(): Promise<ActionResponse> {
await prisma.payment.create({
data: {
userId: user.id,
userId: user.sub,
amount: Math.floor(
Math.random() * (maxAmount - minAmount) + minAmount),
date: date,

View file

@ -1,9 +1,9 @@
import { z } from 'zod';
import { ActionResponse } from '@/lib/types/actionResponse';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { paymentFormSchema } from '@/lib/form-schemas/paymentFormSchema';
import { getSession } from '@auth0/nextjs-auth0';
export default async function paymentCreateUpdate({
id,
@ -16,15 +16,15 @@ export default async function paymentCreateUpdate({
}: z.infer<typeof paymentFormSchema>): Promise<ActionResponse> {
'use server';
// check that user is logged in
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to create/update a payment.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// create/update payment
try {
@ -52,7 +52,7 @@ export default async function paymentCreateUpdate({
} else {
await prisma.payment.create({
data: {
userId: user.id,
userId: user.sub,
amount: amount,
date: date,
payorId: payorId,

View file

@ -1,7 +1,7 @@
import { ActionResponse } from '@/lib/types/actionResponse';
import prisma from '@/prisma';
import { getUser } from '@/auth';
import { URL_SIGN_IN } from '@/lib/constants';
import { getSession } from '@auth0/nextjs-auth0';
export default async function paymentDelete(id: number): Promise<ActionResponse> {
'use server';
@ -14,21 +14,21 @@ export default async function paymentDelete(id: number): Promise<ActionResponse>
};
}
// check that user is logged in
const user = await getUser();
if (!user) {
const session = await getSession();
if (!session) {
return {
type: 'error',
message: 'You must be logged in to delete a payment.',
message: 'You aren\'t signed in.',
redirect: URL_SIGN_IN,
};
}
const user = session.user;
// check that payment is associated with user
const payment = await prisma.payment.findFirst({
where: {
id: id,
userId: user.id,
userId: user.sub,
},
});
if (!payment) {
@ -43,7 +43,7 @@ export default async function paymentDelete(id: number): Promise<ActionResponse>
await prisma.payment.delete({
where: {
id: payment.id,
userId: user.id,
userId: user.sub,
},
},
);

View file

@ -1,4 +1,6 @@
export const URL_SIGN_IN = `/api/auth/login`;
export const URL_SIGN_OUT = `/api/auth/logout`;
// main urls
export const URL_HOME = '/';