From 6e203e7833479b88af2dc65f43cac22a11c9e1a4 Mon Sep 17 00:00:00 2001 From: Hayden <64056131+hay-kot@users.noreply.github.com> Date: Tue, 10 Oct 2023 08:43:44 -0500 Subject: [PATCH] fix: infinite redirect issue (#583) Former-commit-id: ae4b95301f28e361ce989019656f8c78a4469cf9 --- frontend/composables/use-auth-context.ts | 8 ++++---- frontend/middleware/auth.ts | 8 ++++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frontend/composables/use-auth-context.ts b/frontend/composables/use-auth-context.ts index 56fc639..740a303 100644 --- a/frontend/composables/use-auth-context.ts +++ b/frontend/composables/use-auth-context.ts @@ -45,7 +45,8 @@ class AuthContext implements IAuthContext { private _attachmentToken: CookieRef; get token() { - return this._token.value === "true"; + // @ts-ignore sometimes it's a boolean I guess? + return this._token.value === "true" || this._token.value === true; } get attachmentToken() { @@ -66,11 +67,11 @@ class AuthContext implements IAuthContext { } isExpired() { - return this.token; + return !this.token; } isAuthorized() { - return !this.isExpired(); + return this.token; } invalidateSession() { @@ -79,7 +80,6 @@ class AuthContext implements IAuthContext { // Delete the cookies this._token.value = null; this._attachmentToken.value = null; - console.log("Session invalidated"); } diff --git a/frontend/middleware/auth.ts b/frontend/middleware/auth.ts index 25aebeb..2eca73f 100644 --- a/frontend/middleware/auth.ts +++ b/frontend/middleware/auth.ts @@ -3,14 +3,18 @@ export default defineNuxtRouteMiddleware(async () => { const api = useUserApi(); if (!ctx.isAuthorized()) { - return navigateTo("/"); + if (window.location.pathname !== "/") { + return navigateTo("/"); + } } if (!ctx.user) { console.log("Fetching user data"); const { data, error } = await api.user.self(); if (error) { - return navigateTo("/"); + if (window.location.pathname !== "/") { + return navigateTo("/"); + } } ctx.user = data.item;