feat: add auth0 middleware
This commit is contained in:
@@ -1,9 +1,4 @@
|
|||||||
# Water utility portal
|
# Auth0 demo
|
||||||
|
|
||||||
## To do
|
|
||||||
|
|
||||||
- [ ] Get premium Vercel account for database
|
|
||||||
- [ ] Add middleware for authentication
|
|
||||||
|
|
||||||
## Commands
|
## Commands
|
||||||
|
|
||||||
|
|||||||
@@ -1,22 +1,26 @@
|
|||||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
import { getSession } from '@auth0/nextjs-auth0';
|
||||||
import prisma from '@prisma/prisma';
|
import prisma from '@prisma/prisma';
|
||||||
import { createErrorResponse } from '@utils/createErrorResponse';
|
import { createErrorResponse } from '@utils/createErrorResponse';
|
||||||
import { validateApiRequestContext } from '@utils/validateApiRequestContext';
|
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();
|
const session = await getSession();
|
||||||
|
if (!session || !session.user) {
|
||||||
|
return createErrorResponse('Unauthorized', 401);
|
||||||
|
}
|
||||||
|
|
||||||
let params;
|
let params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
params = validateApiRequestContext(context);
|
params = validateApiRequestContext(context);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return createErrorResponse('Internal server error', 500);
|
return createErrorResponse('Internal server error', 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
const userEmail = session?.user?.email;
|
const userEmail = session.user.email;
|
||||||
|
|
||||||
if (!userEmail) {
|
if (!userEmail) {
|
||||||
return createErrorResponse('Session not found or invalid', 401);
|
return createErrorResponse('Session not found or invalid', 401);
|
||||||
}
|
}
|
||||||
@@ -45,24 +49,30 @@ export const GET = withApiAuthRequired(async (_, context) => {
|
|||||||
console.error('Error fetching customer form:', error);
|
console.error('Error fetching customer form:', error);
|
||||||
return createErrorResponse('Internal server error', 500);
|
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();
|
const session = await getSession();
|
||||||
|
if (!session || !session.user) {
|
||||||
|
return createErrorResponse('Unauthorized', 401);
|
||||||
|
}
|
||||||
|
|
||||||
let params;
|
let params;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
params = validateApiRequestContext(context);
|
params = validateApiRequestContext(context);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
return createErrorResponse('Internal server error', 500);
|
return createErrorResponse('Internal server error', 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
const result = await prisma.customerForm.delete({
|
const result = await prisma.customerForm.delete({
|
||||||
where: {
|
where: {
|
||||||
id: params.id,
|
id: params.id,
|
||||||
createdBy: {
|
createdBy: {
|
||||||
email: session?.user.email
|
email: session.user.email
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@@ -75,4 +85,8 @@ export const DELETE = withApiAuthRequired(async (_, context) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return NextResponse.json({ success: true });
|
return NextResponse.json({ success: true });
|
||||||
});
|
} catch (error) {
|
||||||
|
console.error('Error deleting customer form:', error);
|
||||||
|
return createErrorResponse('Internal server error', 500);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
import { getSession, withApiAuthRequired } from '@auth0/nextjs-auth0';
|
import { getSession } from '@auth0/nextjs-auth0';
|
||||||
import { CustomerForm } from '@prisma/client';
|
import { CustomerForm } from '@prisma/client';
|
||||||
import prisma from '@prisma/prisma';
|
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();
|
const session = await getSession();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@@ -25,9 +25,9 @@ export const GET = withApiAuthRequired(async () => {
|
|||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|
||||||
export const POST = withApiAuthRequired(async request => {
|
export async function POST(request: NextRequest) {
|
||||||
try {
|
try {
|
||||||
const session = await getSession();
|
const session = await getSession();
|
||||||
|
|
||||||
@@ -59,4 +59,4 @@ export const POST = withApiAuthRequired(async request => {
|
|||||||
{ status: 500 }
|
{ status: 500 }
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
}
|
||||||
|
|||||||
@@ -1,12 +1,12 @@
|
|||||||
import { NextResponse } from 'next/server';
|
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();
|
const session = await getSession();
|
||||||
|
|
||||||
return NextResponse.json({
|
return NextResponse.json({
|
||||||
success: true,
|
success: true,
|
||||||
data: { email: session?.user.email }
|
data: { email: session?.user.email }
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { withPageAuthRequired } from '@auth0/nextjs-auth0/client';
|
|
||||||
import { CustomerForm, CustomerFormSchema } from '@utils/types';
|
import { CustomerForm, CustomerFormSchema } from '@utils/types';
|
||||||
import axios from 'axios';
|
import axios from 'axios';
|
||||||
import { useRouter } from 'next/navigation';
|
import { useRouter } from 'next/navigation';
|
||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
|
|
||||||
export default withPageAuthRequired(function SingleCustomerForm({
|
export default function SingleCustomerForm({
|
||||||
params
|
params
|
||||||
}: {
|
}: {
|
||||||
params: { id: string };
|
params: { id: string };
|
||||||
@@ -75,4 +74,4 @@ export default withPageAuthRequired(function SingleCustomerForm({
|
|||||||
</button>
|
</button>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import { withPageAuthRequired } from '@auth0/nextjs-auth0/client';
|
|
||||||
import { Button } from '@components/Button';
|
import { Button } from '@components/Button';
|
||||||
import { FormControl } from '@components/FormControl';
|
import { FormControl } from '@components/FormControl';
|
||||||
import { FormMessage } from '@components/FormMessage';
|
import { FormMessage } from '@components/FormMessage';
|
||||||
@@ -21,7 +20,7 @@ import { useRouter } from 'next/navigation';
|
|||||||
import { useEffect, useState } from 'react';
|
import { useEffect, useState } from 'react';
|
||||||
import { FormProvider, useForm } from 'react-hook-form';
|
import { FormProvider, useForm } from 'react-hook-form';
|
||||||
|
|
||||||
export default withPageAuthRequired(function CustomerForms() {
|
export default function CustomerForms() {
|
||||||
const router = useRouter();
|
const router = useRouter();
|
||||||
const [customerForms, setCustomerForms] = useState<CustomerForm[]>([]);
|
const [customerForms, setCustomerForms] = useState<CustomerForm[]>([]);
|
||||||
|
|
||||||
@@ -139,4 +138,4 @@ export default withPageAuthRequired(function CustomerForms() {
|
|||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
});
|
}
|
||||||
|
|||||||
14
middleware.ts
Normal file
14
middleware.ts
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
import { withMiddlewareAuthRequired } from '@auth0/nextjs-auth0/edge';
|
||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
export default withMiddlewareAuthRequired({
|
||||||
|
async middleware() {
|
||||||
|
const res = NextResponse.next();
|
||||||
|
return res;
|
||||||
|
},
|
||||||
|
returnTo: '/api/auth/login'
|
||||||
|
});
|
||||||
|
|
||||||
|
export const config = {
|
||||||
|
matcher: ['/api/protected/:path*', '/customer-form/:path*']
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user