From a813dc0c7395ad6cc0a19ebb84e4bb8e54ca9f18 Mon Sep 17 00:00:00 2001 From: ='fauz <='fauzgabriel@gmail.com> Date: Thu, 30 Oct 2025 15:18:43 +0700 Subject: [PATCH] auth change method --- src/router/index.js | 25 +++++++++++++++++-------- src/stores/auth.js | 2 +- 2 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/router/index.js b/src/router/index.js index 91c449b..6314184 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -52,16 +52,25 @@ const router = createRouter({ router.beforeEach(async(to, from, next) => { const auth = useAuthStore(); if (!auth.isLoggedIn) { - auth.restoreSession(); + // auth.restoreSession(); + try { + await auth.fetchUser(); + } catch (e) { + console.log("Token tidak valid / expired."); + } } - const isLoggedIn = auth.isLoggedIn; - console.log(isLoggedIn) - if(to.meta.requiresAuth && !isLoggedIn) { - return next('/login'); - } - if (to.path === '/login' && isLoggedIn) { - return next('/') + // const isLoggedIn = auth.isLoggedIn; + // console.log(isLoggedIn) + // if(to.meta.requiresAuth && !isLoggedIn) { + // return next('/login'); + // } + // if (to.path === '/login' && isLoggedIn) { + // return next('/') + // } + + if (to.meta.requiresAuth && !auth.isLoggedIn) { + return next("/login"); } return next(); diff --git a/src/stores/auth.js b/src/stores/auth.js index 0647c6b..b68ef37 100644 --- a/src/stores/auth.js +++ b/src/stores/auth.js @@ -11,7 +11,7 @@ export const useAuthStore = defineStore('auth', { user: null }), getters: { - isLoggedIn: (state) => !!state.token, + isLoggedIn: (state) => !!state.user !== null, // isLoggedIn: (state) => !!state.token && !!state.user, }, actions: {