From c0598bf5b4b74345d777ccdf8b173e64bf86ed36 Mon Sep 17 00:00:00 2001 From: swve Date: Tue, 13 Aug 2024 18:45:23 +0200 Subject: [PATCH] fix: login without cookies auth issues fix --- apps/web/middleware.ts | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/apps/web/middleware.ts b/apps/web/middleware.ts index 829a905a..6cae7a10 100644 --- a/apps/web/middleware.ts +++ b/apps/web/middleware.ts @@ -42,14 +42,25 @@ export default async function middleware(req: NextRequest) { // Redirect to the same pathname with the original search params return NextResponse.rewrite(new URL(`${pathname}${search}`, req.url)) } - if (auth_paths.includes(pathname)) { - // Redirect to the same pathname with the original search params - return NextResponse.rewrite(new URL(`/auth${pathname}${search}`, req.url)) - } - // Login - if (orgslug == 'auth' || pathname.startsWith('/login')) { - return NextResponse.rewrite(new URL(`/login${search}`, req.url)) + if (auth_paths.includes(pathname)) { + const response = NextResponse.rewrite( + new URL(`/auth${pathname}${search}`, req.url) + ) + + // Parse the search params + const searchParams = new URLSearchParams(search) + const orgslug = searchParams.get('orgslug') + + if (orgslug) { + response.cookies.set({ + name: 'learnhouse_current_orgslug', + value: orgslug, + domain: + LEARNHOUSE_TOP_DOMAIN == 'localhost' ? '' : LEARNHOUSE_TOP_DOMAIN, + }) + } + return response } // Install Page (depreceated) @@ -84,6 +95,7 @@ export default async function middleware(req: NextRequest) { } return NextResponse.redirect(redirectUrl) } else { + return 'Did not find the orgslug in the cookie' } }