From 12c689d1d666745eece6a1024325ad2ed921b8dc Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Thu, 4 Apr 2024 00:48:36 +0200 Subject: [PATCH] N-FIN-79: refactor server actions to use auth0 session --- src/lib/actions/accountDelete.ts | 40 +++++++++------------ src/lib/actions/categoryCreateUpdate.ts | 12 +++---- src/lib/actions/categoryDelete.ts | 14 ++++---- src/lib/actions/entityCreateUpdate.ts | 12 +++---- src/lib/actions/entityDelete.ts | 14 ++++---- src/lib/actions/generateSampleData.ts | 46 ++++++++++++------------- src/lib/actions/paymentCreateUpdate.ts | 12 +++---- src/lib/actions/paymentDelete.ts | 14 ++++---- src/lib/constants.ts | 2 ++ 9 files changed, 81 insertions(+), 85 deletions(-) diff --git a/src/lib/actions/accountDelete.ts b/src/lib/actions/accountDelete.ts index 952e31c..caf0f3c 100644 --- a/src/lib/actions/accountDelete.ts +++ b/src/lib/actions/accountDelete.ts @@ -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 { '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, }; } diff --git a/src/lib/actions/categoryCreateUpdate.ts b/src/lib/actions/categoryCreateUpdate.ts index 11bdd5f..aebadc8 100644 --- a/src/lib/actions/categoryCreateUpdate.ts +++ b/src/lib/actions/categoryCreateUpdate.ts @@ -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): Promise { '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, }, diff --git a/src/lib/actions/categoryDelete.ts b/src/lib/actions/categoryDelete.ts index 8773fe3..24832ff 100644 --- a/src/lib/actions/categoryDelete.ts +++ b/src/lib/actions/categoryDelete.ts @@ -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 { 'use server'; @@ -14,21 +14,21 @@ export default async function categoryDelete(id: number): Promise): Promise { '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, diff --git a/src/lib/actions/entityDelete.ts b/src/lib/actions/entityDelete.ts index 2e0e3eb..fccc6fd 100644 --- a/src/lib/actions/entityDelete.ts +++ b/src/lib/actions/entityDelete.ts @@ -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 { 'use server'; @@ -14,21 +14,21 @@ export default async function entityDelete(id: number): Promise }; } - // 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 await prisma.entity.delete({ where: { id: entity.id, - userId: user.id, + userId: user.sub, }, }, ); diff --git a/src/lib/actions/generateSampleData.ts b/src/lib/actions/generateSampleData.ts index 09ea36b..8b16d17 100644 --- a/src/lib/actions/generateSampleData.ts +++ b/src/lib/actions/generateSampleData.ts @@ -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 { '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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { 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 { // 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 { 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 { await prisma.payment.create({ data: { - userId: user.id, + userId: user.sub, amount: Math.floor( Math.random() * (maxAmount - minAmount) + minAmount), date: date, diff --git a/src/lib/actions/paymentCreateUpdate.ts b/src/lib/actions/paymentCreateUpdate.ts index 4a66d33..4c304a9 100644 --- a/src/lib/actions/paymentCreateUpdate.ts +++ b/src/lib/actions/paymentCreateUpdate.ts @@ -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): Promise { '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, diff --git a/src/lib/actions/paymentDelete.ts b/src/lib/actions/paymentDelete.ts index a3bfd16..dab905c 100644 --- a/src/lib/actions/paymentDelete.ts +++ b/src/lib/actions/paymentDelete.ts @@ -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 { 'use server'; @@ -14,21 +14,21 @@ export default async function paymentDelete(id: number): Promise }; } - // 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 await prisma.payment.delete({ where: { id: payment.id, - userId: user.id, + userId: user.sub, }, }, ); diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 1693930..c88c2ee 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -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 = '/';