From 3d3952adc56a11a19245d783ec27da0231e2b291 Mon Sep 17 00:00:00 2001 From: yashksaini-coder Date: Mon, 3 Mar 2025 21:21:12 +0530 Subject: [PATCH 1/2] =?UTF-8?q?refactor:=20=F0=9F=9B=A0=EF=B8=8F=20Improve?= =?UTF-8?q?=20authentication=20flow=20by=20using=20window.location=20for?= =?UTF-8?q?=20reliable=20redirection=20after=20sign-in=20and=20sign-out?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- store/AuthStore/useAuthStore.ts | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/store/AuthStore/useAuthStore.ts b/store/AuthStore/useAuthStore.ts index d1ba178..9dcba2d 100644 --- a/store/AuthStore/useAuthStore.ts +++ b/store/AuthStore/useAuthStore.ts @@ -28,7 +28,7 @@ interface authStore { export const useAuthStore = create((set) => ({ signinError: null, isSigningIn: false, - signin: async (signinMetaData,router) => { + signin: async (signinMetaData, router) => { const supabase = createClient() set({ isSigningIn: true, signinError: null }) try { @@ -42,8 +42,9 @@ export const useAuthStore = create((set) => ({ } if (data.session) { - // Ensure we have a session before redirecting - await router.push('/dashboard'); + // For reliable redirection, reload the page instead of using router.push + // This ensures the middleware properly detects the authentication state + window.location.href = '/dashboard'; } else { throw new Error("Unable to retrieve session after login."); } @@ -59,7 +60,8 @@ export const useAuthStore = create((set) => ({ const supabase = createClient() try { await supabase.auth.signOut(); - router.push('/auth/signin'); + // Use window.location for reliable redirection after logout + window.location.href = '/auth/signin'; } catch (error) { console.error('Logout error:', error); } From 7ec7d7e5507a7b3d13803a1f841ef1159e9d8ad1 Mon Sep 17 00:00:00 2001 From: yashksaini-coder Date: Mon, 3 Mar 2025 21:22:01 +0530 Subject: [PATCH 2/2] =?UTF-8?q?refactor:=20=F0=9F=9B=A0=EF=B8=8F=20Enhance?= =?UTF-8?q?=20authentication=20middleware=20to=20improve=20user=20redirect?= =?UTF-8?q?ion=20logic=20and=20prevent=20redirect=20loops?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/supabase/middleware.ts | 36 ++++-------------------------------- 1 file changed, 4 insertions(+), 32 deletions(-) diff --git a/utils/supabase/middleware.ts b/utils/supabase/middleware.ts index 9b39d6d..0ebcb76 100644 --- a/utils/supabase/middleware.ts +++ b/utils/supabase/middleware.ts @@ -27,61 +27,33 @@ export async function updateSession(request: NextRequest) { } ) - // Do not run code between createServerClient and - // supabase.auth.getUser(). A simple mistake could make it very hard to debug - // issues with users being randomly logged out. - // IMPORTANT: DO NOT REMOVE auth.getUser() - const { data: { user }, } = await supabase.auth.getUser() - // if ( - // !user && - // !request.nextUrl.pathname.startsWith('/login') && - // !request.nextUrl.pathname.startsWith('/auth') - // ) { - // // no user, potentially respond by redirecting the user to the login page - // const url = request.nextUrl.clone() - // url.pathname = '/login' - // return NextResponse.redirect(url) - // } + // Protected routes check - redirect to signin if not authenticated if ( !user && (request.nextUrl.pathname.startsWith('/dashboard')) ) { - // no user, potentially respond by redirecting the user to the login page const url = request.nextUrl.clone() url.pathname = '/auth/signin' return NextResponse.redirect(url) } - - + // Redirect authenticated users away from auth pages if ( user && (request.nextUrl.pathname === '/' || request.nextUrl.pathname.startsWith('/auth/signin') || request.nextUrl.pathname.startsWith('/auth/signup')) ) { - // user exists and route is / or /auth/signin or /auth/signup, redirect to /dashboard + // Add a cache-busting parameter to avoid redirect loops const url = request.nextUrl.clone() url.pathname = '/dashboard' + url.searchParams.set('_ts', Date.now().toString()) return NextResponse.redirect(url) } - // IMPORTANT: You *must* return the supabaseResponse object as it is. - // If you're creating a new response object with NextResponse.next() make sure to: - // 1. Pass the request in it, like so: - // const myNewResponse = NextResponse.next({ request }) - // 2. Copy over the cookies, like so: - // myNewResponse.cookies.setAll(supabaseResponse.cookies.getAll()) - // 3. Change the myNewResponse object to fit your needs, but avoid changing - // the cookies! - // 4. Finally: - // return myNewResponse - // If this is not done, you may be causing the browser and server to go out - // of sync and terminate the user's session prematurely! - return supabaseResponse } \ No newline at end of file