feat: add auth0 middleware
This commit is contained in:
@@ -1,22 +1,26 @@
|
||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
||||
import { getSession } from '@auth0/nextjs-auth0';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { createErrorResponse } from '@utils/createErrorResponse';
|
||||
import { validateApiRequestContext } from '@utils/validateApiRequestContext';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const GET = withApiAuthRequired(async (_, context) => {
|
||||
export async function GET(
|
||||
request: NextRequest,
|
||||
context: { params: { id: string } }
|
||||
) {
|
||||
const session = await getSession();
|
||||
if (!session || !session.user) {
|
||||
return createErrorResponse('Unauthorized', 401);
|
||||
}
|
||||
|
||||
let params;
|
||||
|
||||
try {
|
||||
params = validateApiRequestContext(context);
|
||||
} catch (error) {
|
||||
return createErrorResponse('Internal server error', 500);
|
||||
}
|
||||
|
||||
const userEmail = session?.user?.email;
|
||||
|
||||
const userEmail = session.user.email;
|
||||
if (!userEmail) {
|
||||
return createErrorResponse('Session not found or invalid', 401);
|
||||
}
|
||||
@@ -45,34 +49,44 @@ export const GET = withApiAuthRequired(async (_, context) => {
|
||||
console.error('Error fetching customer form:', error);
|
||||
return createErrorResponse('Internal server error', 500);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const DELETE = withApiAuthRequired(async (_, context) => {
|
||||
export async function DELETE(
|
||||
request: NextRequest,
|
||||
context: { params: { id: string } }
|
||||
) {
|
||||
const session = await getSession();
|
||||
if (!session || !session.user) {
|
||||
return createErrorResponse('Unauthorized', 401);
|
||||
}
|
||||
|
||||
let params;
|
||||
|
||||
try {
|
||||
params = validateApiRequestContext(context);
|
||||
} catch (error) {
|
||||
return createErrorResponse('Internal server error', 500);
|
||||
}
|
||||
|
||||
const result = await prisma.customerForm.delete({
|
||||
where: {
|
||||
id: params.id,
|
||||
createdBy: {
|
||||
email: session?.user.email
|
||||
try {
|
||||
const result = await prisma.customerForm.delete({
|
||||
where: {
|
||||
id: params.id,
|
||||
createdBy: {
|
||||
email: session.user.email
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Something went wrong.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
if (!result) {
|
||||
return NextResponse.json(
|
||||
{ success: false, message: 'Something went wrong.' },
|
||||
{ status: 500 }
|
||||
);
|
||||
return NextResponse.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Error deleting customer form:', error);
|
||||
return createErrorResponse('Internal server error', 500);
|
||||
}
|
||||
|
||||
return NextResponse.json({ success: true });
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,9 +1,9 @@
|
||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
||||
import { getSession } from '@auth0/nextjs-auth0';
|
||||
import { CustomerForm } from '@prisma/client';
|
||||
import prisma from '@prisma/prisma';
|
||||
import { NextResponse } from 'next/server';
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
|
||||
export const GET = withApiAuthRequired(async () => {
|
||||
export async function GET() {
|
||||
const session = await getSession();
|
||||
|
||||
try {
|
||||
@@ -25,9 +25,9 @@ export const GET = withApiAuthRequired(async () => {
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export const POST = withApiAuthRequired(async request => {
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const session = await getSession();
|
||||
|
||||
@@ -59,4 +59,4 @@ export const POST = withApiAuthRequired(async request => {
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { NextResponse } from 'next/server';
|
||||
|
||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
||||
import { getSession } from '@auth0/nextjs-auth0';
|
||||
|
||||
export const GET = withApiAuthRequired(async () => {
|
||||
export async function GET() {
|
||||
const session = await getSession();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: { email: session?.user.email }
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user