diff --git a/app/api/auth/callback/route.ts b/app/api/auth/callback/route.ts index 022aa8d..5cb2af5 100644 --- a/app/api/auth/callback/route.ts +++ b/app/api/auth/callback/route.ts @@ -7,7 +7,8 @@ export async function POST(request: NextRequest) { const code = form.get('code') // pass code to the backend - const loginEndpoint = process.env.NEXT_PUBLIC_API_URL + '/login' + const backendURL = process.env.NEXT_PUBLIC_API_INTERNAL_URL ? process.env.NEXT_PUBLIC_API_INTERNAL_URL : process.env.NEXT_PUBLIC_API_URL + const loginEndpoint = backendURL + '/login' // redirect uri const redirectUri = process.env.NEXT_PUBLIC_OIDC_REDIRECT_URL @@ -26,13 +27,13 @@ export async function POST(request: NextRequest) { // get cookie from response const cookie = response.headers.get('set-cookie') - + const subDirectory = process.env.SUB_DIRECTORY ? process.env.SUB_DIRECTORY : "/" if (cookie) { // redirect to root page return new Response(null, { status: 301, headers: { - "Location": '/', + "Location": subDirectory, "Set-Cookie": cookie, }, }) @@ -42,7 +43,7 @@ export async function POST(request: NextRequest) { return new Response(null, { status: 301, headers: { - "Location": '/', + "Location": subDirectory, }, }) } diff --git a/middleware.ts b/middleware.ts index 1695f1f..09cc6a5 100644 --- a/middleware.ts +++ b/middleware.ts @@ -14,7 +14,13 @@ export function middleware(request: NextRequest) { // check if next path starts with any of the protected paths or root if (protectedPaths.some(path => request.nextUrl.pathname.startsWith(path)) || request.nextUrl.pathname === '/') { - return NextResponse.redirect(new URL('/login', request.url)); + const subDirectory = process.env.SUB_DIRECTORY + if (subDirectory) { + return NextResponse.redirect(new URL(subDirectory + '/login', request.url)); + } + else { + return NextResponse.redirect(new URL('/login', request.url)); + } } } diff --git a/src/lib/ApiClient.ts b/src/lib/ApiClient.ts index 9b13211..132e9fe 100644 --- a/src/lib/ApiClient.ts +++ b/src/lib/ApiClient.ts @@ -1,4 +1,5 @@ const ApiUrl = process.env.NEXT_PUBLIC_API_URL; +const ApiInternalUrl = process.env.NEXT_PUBLIC_API_INTERNAL_URL ? process.env.NEXT_PUBLIC_API_INTERNAL_URL : ApiUrl; const headers = { "Content-Type": "application/json", @@ -24,7 +25,7 @@ async function fetchWithToken(url: string, options: RequestInit | undefined = {} } try { - const fullUrl = `${ApiUrl}${url}` + const fullUrl = typeof window === 'undefined' ? `${ApiInternalUrl}${url}` : `${ApiUrl}${url}` const response = await fetch(fullUrl, mergedOptions); // Check if the response was ok (status in the range 200-299)