🛡️Authenticated API
Protect API routes from unauthenticated access
NextAuth
import { getServerSession } from "next-auth/next";
/* ... */
export async function GET() {
// retrieve the current session
const session = await getServerSession(authOptions);
// check if the session exists and user email is set
if (!session || !session?.user?.email) {
return NextResponse.json(
{ error: "Unauthorized" },
{ status: HttpStatusCode.Unauthorized }
);
}
}Supabase Auth
Last updated