feat: login store moved to sessionStorage

This commit is contained in:
2025-10-31 00:24:20 +01:00
parent f1468ede1a
commit bbf58ecb19
4 changed files with 58 additions and 20 deletions

View File

@@ -1,8 +1,43 @@
import { USER_SESSION_STORAGE_KEY } from "~/constants/session.constants";
import type { User } from "~/stores/user";
export default defineNuxtRouteMiddleware((to, from) => {
let session: string | null = null;
// Not secured routes
if (
to.path.startsWith("/favicon.ico") ||
to.path.startsWith("/assets/") ||
to.path.startsWith("/login")
) {
return;
}
const userStore = useUserStore();
console.log(
">> Global Middleware from ",
from.query,
userStore.profile,
);
userStore.update({ id: "test", roles: ["admin"] });
// Recover user session
// NOTE: using the sessionStorage will not allow the user to have multiple tabs, to communicate them one can use the
// broadcast channel: https://developer.mozilla.org/en-US/docs/Web/API/Broadcast_Channel_API
try {
session = sessionStorage.getItem(
USER_SESSION_STORAGE_KEY,
);
} catch (e) {
console.error(">> [!] Not in client");
}
// Manage secured routes
if (session) {
const user = JSON.parse(session) as User;
if (
user.apiSession?.exp &&
user.apiSession.exp > Date.now()
) {
// try refresh token
// else just redirect to login
}
}
console.log(">> Global Middleware from ", from.path);
});

View File

@@ -1,3 +1,3 @@
export default defineNuxtRouteMiddleware((to, from) => {
console.log(">> Middleware from ", from.query);
})
console.log(">> Login middleware from ", from.path);
});