Skip to content
This repository has been archived by the owner on Jun 12, 2024. It is now read-only.

Commit

Permalink
fix: infinite redirect issue (#583)
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot authored Oct 10, 2023
1 parent d8482f3 commit ae4b953
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 4 additions & 4 deletions frontend/composables/use-auth-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ class AuthContext implements IAuthContext {
private _attachmentToken: CookieRef<string | null>;

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() {
Expand All @@ -66,11 +67,11 @@ class AuthContext implements IAuthContext {
}

isExpired() {
return this.token;
return !this.token;
}

isAuthorized() {
return !this.isExpired();
return this.token;
}

invalidateSession() {
Expand All @@ -79,7 +80,6 @@ class AuthContext implements IAuthContext {
// Delete the cookies
this._token.value = null;
this._attachmentToken.value = null;

console.log("Session invalidated");
}

Expand Down
8 changes: 6 additions & 2 deletions frontend/middleware/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit ae4b953

Please sign in to comment.