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

fix infinite redirect issue #583

Merged
merged 1 commit into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading