Merge pull request #211 from SELab-2/fix/login-state-nieuw-tabblad-#210
fix: Wanneer men onze applicatie in een tweede tab opent, is men daar niet ingelogd.
This commit is contained in:
commit
e2749aace3
4 changed files with 18 additions and 4 deletions
|
@ -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 {
|
||||
|
|
|
@ -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");
|
||||
|
|
|
@ -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();
|
||||
|
|
|
@ -37,7 +37,20 @@ 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 (_: 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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue