Let's dive into creating API routes with Next.js and ensuring they are authenticated using auth()
function.
1 import { NextRequest, NextResponse } from 'next/server';
2 export const GET = async (request: NextRequest) => {
3 .....
4 return new NextResponse();
5 }
auth()
function to authenticate your API routes. This ensures that only authenticated users can access them.1 import { NextRequest, NextResponse } from 'next/server';
2 import { auth } from '@/lib/next-auth';
3 export const POST = auth(async (request: NextRequest) => {
4 .....
5 return new NextResponse();
6 });