feat: attempt to fix the crud with auth
This commit is contained in:
22
app/api/protected/customer-form/[id]/route.ts
Normal file
22
app/api/protected/customer-form/[id]/route.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: number } }
|
||||
) {
|
||||
return NextResponse.json(`GET ${params.id}`);
|
||||
}
|
||||
|
||||
export async function PUT(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: number } }
|
||||
) {
|
||||
return NextResponse.json(`PUT ${params.id}`);
|
||||
}
|
||||
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
{ params }: { params: { id: number } }
|
||||
) {
|
||||
return NextResponse.json(`DELETE ${params.id}`);
|
||||
}
|
||||
53
app/api/protected/customer-form/route.ts
Normal file
53
app/api/protected/customer-form/route.ts
Normal file
@@ -0,0 +1,53 @@
|
||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { randomUUID } from 'crypto';
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
export const GET = withApiAuthRequired(async () => {
|
||||
const session = await getSession();
|
||||
|
||||
console.log('GET', session?.user);
|
||||
|
||||
// const userModules = await prisma.user.findUniqueOrThrow({
|
||||
// where: {
|
||||
// email: session?.user.email
|
||||
// },
|
||||
// include: {
|
||||
// CustomerForm: true
|
||||
// }
|
||||
// });
|
||||
|
||||
// const customerForms: CustomerForm[] = userModules.CustomerForm;
|
||||
|
||||
return NextResponse.json([]);
|
||||
});
|
||||
|
||||
export const POST = withApiAuthRequired(async request => {
|
||||
try {
|
||||
const session = await getSession();
|
||||
|
||||
const body = await request.json();
|
||||
|
||||
console.log('POST', session?.user);
|
||||
|
||||
const newCustomerForm = await prisma.customerForm.create({
|
||||
data: {
|
||||
type: body.type,
|
||||
text: body.text,
|
||||
createdBy: {
|
||||
connect: {
|
||||
id: randomUUID(),
|
||||
email: session?.user.email
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true, data: newCustomerForm });
|
||||
} catch (error) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Something went wrong.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
Reference in New Issue
Block a user