From 8e7ecb18817059056cca12f478eb85cac0184802 Mon Sep 17 00:00:00 2001 From: atrincas Date: Fri, 17 Jan 2025 09:19:39 +0100 Subject: [PATCH] added cors headers to auth api routes --- client/src/app/auth/api/session/route.ts | 13 +++++-- client/src/app/auth/api/signin/route.ts | 49 ++++++++++++++++-------- client/src/app/auth/api/signout/route.ts | 13 +++++-- client/src/lib/auth/server.ts | 19 ++++++++- 4 files changed, 69 insertions(+), 25 deletions(-) diff --git a/client/src/app/auth/api/session/route.ts b/client/src/app/auth/api/session/route.ts index b2e07e07..5c0a5feb 100644 --- a/client/src/app/auth/api/session/route.ts +++ b/client/src/app/auth/api/session/route.ts @@ -1,6 +1,7 @@ import { NextResponse } from "next/server"; import { getServerSession } from "@/lib/auth/server"; +import { getCorsHeaders } from "@/lib/auth/server"; import { AuthApiResponse } from "@/lib/auth/types"; import { AppSession } from "@/lib/auth/types"; @@ -8,9 +9,13 @@ export async function GET(): Promise< NextResponse> > { const session = await getServerSession(); + const corsHeaders = await getCorsHeaders("GET"); - return NextResponse.json({ - body: session || null, - status: session ? 200 : 401, - }); + return NextResponse.json( + { + body: session || null, + status: session ? 200 : 401, + }, + { headers: corsHeaders }, + ); } diff --git a/client/src/app/auth/api/signin/route.ts b/client/src/app/auth/api/signin/route.ts index 88b1e331..79754842 100644 --- a/client/src/app/auth/api/signin/route.ts +++ b/client/src/app/auth/api/signin/route.ts @@ -1,7 +1,11 @@ import { NextRequest, NextResponse } from "next/server"; import { generateUserJWT } from "@/lib/auth/jwt"; -import { setAuthCookie, setResponseCookie } from "@/lib/auth/server"; +import { + setAuthCookie, + setResponseCookie, + getCorsHeaders, +} from "@/lib/auth/server"; import { AuthApiResponse, AppSession } from "@/lib/auth/types"; import { client } from "@/lib/query-client"; @@ -10,17 +14,21 @@ export async function POST( ): Promise>> { try { const { email, password } = await req.json(); + const corsHeaders = await getCorsHeaders("POST"); const response = await client.auth.login.mutation({ body: { email, password }, }); if (response.status !== 201) { - return NextResponse.json({ - body: null, - status: response.status, - error: response.body.errors?.[0]?.title || "Invalid credentials", - }); + return NextResponse.json( + { + body: null, + status: response.status, + error: response.body.errors?.[0]?.title || "Invalid credentials", + }, + { headers: corsHeaders }, + ); } setResponseCookie(response.headers); @@ -29,15 +37,26 @@ export async function POST( const token = await generateUserJWT(appSession); setAuthCookie(token); - return NextResponse.json({ - body: appSession, - status: 201, - }); + return NextResponse.json( + { + body: appSession, + status: 201, + }, + { headers: corsHeaders }, + ); } catch (err) { - return NextResponse.json({ - body: null, - status: 500, - error: "An error occurred during sign in", - }); + const corsHeaders = await getCorsHeaders("POST"); + return NextResponse.json( + { + body: null, + status: 500, + error: "An error occurred during sign in", + }, + { headers: corsHeaders }, + ); } } + +export async function OPTIONS(): Promise { + return NextResponse.json({}, { headers: await getCorsHeaders("POST") }); +} diff --git a/client/src/app/auth/api/signout/route.ts b/client/src/app/auth/api/signout/route.ts index 353b515c..a4c0ff77 100644 --- a/client/src/app/auth/api/signout/route.ts +++ b/client/src/app/auth/api/signout/route.ts @@ -1,13 +1,18 @@ import { NextResponse } from "next/server"; import { revokeSession } from "@/lib/auth/server"; +import { getCorsHeaders } from "@/lib/auth/server"; import { AuthApiResponse } from "@/lib/auth/types"; export async function POST(): Promise>> { await revokeSession(); + const corsHeaders = await getCorsHeaders("POST"); - return NextResponse.json({ - body: null, - status: 200, - }); + return NextResponse.json( + { + body: null, + status: 200, + }, + { headers: corsHeaders }, + ); } diff --git a/client/src/lib/auth/server.ts b/client/src/lib/auth/server.ts index 6ce4e9e6..7bbb8aba 100644 --- a/client/src/lib/auth/server.ts +++ b/client/src/lib/auth/server.ts @@ -41,10 +41,15 @@ export async function revokeSession(): Promise { cookies().delete(TOKEN_KEY); } -export async function getServerAuthUrl(): Promise { +export async function getServerOrigin(): Promise { const host = headers().get("host"); const protocol = process.env.NODE_ENV === "development" ? "http" : "https"; - return `${protocol}://${host}/auth/api`; + return `${protocol}://${host}`; +} + +export async function getServerAuthUrl(): Promise { + const origin = await getServerOrigin(); + return `${origin}/auth/api`; } /** @@ -67,3 +72,13 @@ export async function setResponseCookie(headers: Headers): Promise { }); } } + +export async function getCorsHeaders(methods: string): Promise { + const origin = await getServerOrigin(); + + return { + "Access-Control-Allow-Origin": origin, + "Access-Control-Allow-Methods": methods, + "Access-Control-Allow-Headers": "Content-Type, Authorization", + }; +}