fix(frontend): Login state blijft behouden voor andere tabs binnen dezelfde browser.

This commit is contained in:
Gerald Schmittinger 2025-04-22 11:04:24 +02:00
parent 37c9e622e6
commit 2578555ace
4 changed files with 17 additions and 4 deletions

View file

@ -3,6 +3,9 @@
import MenuBar from "@/components/MenuBar.vue";
import { useRoute } from "vue-router";
import { computed } from "vue";
import authService from "@/services/auth/auth-service.ts";
void authService.loadUser();
const route = useRoute();
interface RouteMeta {

View file

@ -12,11 +12,9 @@ import App from "./App.vue";
import router from "./router";
import { aliases, mdi } from "vuetify/iconsets/mdi";
import { VueQueryPlugin, QueryClient } from "@tanstack/vue-query";
import authService from "./services/auth/auth-service.ts";
const app = createApp(App);
await authService.loadUser();
app.use(router);
const link = document.createElement("link");

View file

@ -138,7 +138,7 @@ const router = createRouter({
router.beforeEach(async (to, _from, next) => {
// Verify if user is logged in before accessing certain routes
if (to.meta.requiresAuth) {
if (!authService.isLoggedIn.value) {
if (!authService.isLoggedIn.value && !await authService.loadUser()) {
next("/login");
} else {
next();

View file

@ -37,7 +37,19 @@ async function loadUser(): Promise<User | null> {
if (!activeRole) {
return null;
}
const user = await (await getUserManagers())[activeRole].getUser();
const userManager = (await getUserManagers())[activeRole];
let user = await userManager.getUser(); // Load the user from the local storage.
if (!user) { // If the user is not in the local storage, he could still be authenticated in Keycloak.
try {
user = await userManager.signinSilent()
} catch (e: unknown) {
// When the user was previously logged in and then logged out, signinSilent throws an error.
// In that case, the user is not authenticated anymore, so we set him to null.
user = null;
}
}
setUserAuthInfo(user);
authState.activeRole = activeRole ?? null;
return user;