Skip to content

Commit 3d3952a

Browse files
refactor: 🛠️ Improve authentication flow by using window.location for reliable redirection after sign-in and sign-out
1 parent d705f63 commit 3d3952a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

‎store/AuthStore/useAuthStore.ts

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ interface authStore {
2828
export const useAuthStore = create<authStore>((set) => ({
2929
signinError: null,
3030
isSigningIn: false,
31-
signin: async (signinMetaData,router) => {
31+
signin: async (signinMetaData, router) => {
3232
const supabase = createClient()
3333
set({ isSigningIn: true, signinError: null })
3434
try {
@@ -42,8 +42,9 @@ export const useAuthStore = create<authStore>((set) => ({
4242
}
4343

4444
if (data.session) {
45-
// Ensure we have a session before redirecting
46-
await router.push('/dashboard');
45+
// For reliable redirection, reload the page instead of using router.push
46+
// This ensures the middleware properly detects the authentication state
47+
window.location.href = '/dashboard';
4748
} else {
4849
throw new Error("Unable to retrieve session after login.");
4950
}
@@ -59,7 +60,8 @@ export const useAuthStore = create<authStore>((set) => ({
5960
const supabase = createClient()
6061
try {
6162
await supabase.auth.signOut();
62-
router.push('/auth/signin');
63+
// Use window.location for reliable redirection after logout
64+
window.location.href = '/auth/signin';
6365
} catch (error) {
6466
console.error('Logout error:', error);
6567
}

0 commit comments

Comments
 (0)