auth change method
This commit is contained in:
parent
e3f9fc6789
commit
a813dc0c73
@ -52,16 +52,25 @@ const router = createRouter({
|
|||||||
router.beforeEach(async(to, from, next) => {
|
router.beforeEach(async(to, from, next) => {
|
||||||
const auth = useAuthStore();
|
const auth = useAuthStore();
|
||||||
if (!auth.isLoggedIn) {
|
if (!auth.isLoggedIn) {
|
||||||
auth.restoreSession();
|
// auth.restoreSession();
|
||||||
|
try {
|
||||||
|
await auth.fetchUser();
|
||||||
|
} catch (e) {
|
||||||
|
console.log("Token tidak valid / expired.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const isLoggedIn = auth.isLoggedIn;
|
// const isLoggedIn = auth.isLoggedIn;
|
||||||
console.log(isLoggedIn)
|
// console.log(isLoggedIn)
|
||||||
if(to.meta.requiresAuth && !isLoggedIn) {
|
// if(to.meta.requiresAuth && !isLoggedIn) {
|
||||||
return next('/login');
|
// return next('/login');
|
||||||
}
|
// }
|
||||||
if (to.path === '/login' && isLoggedIn) {
|
// if (to.path === '/login' && isLoggedIn) {
|
||||||
return next('/')
|
// return next('/')
|
||||||
|
// }
|
||||||
|
|
||||||
|
if (to.meta.requiresAuth && !auth.isLoggedIn) {
|
||||||
|
return next("/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
|
|||||||
@ -11,7 +11,7 @@ export const useAuthStore = defineStore('auth', {
|
|||||||
user: null
|
user: null
|
||||||
}),
|
}),
|
||||||
getters: {
|
getters: {
|
||||||
isLoggedIn: (state) => !!state.token,
|
isLoggedIn: (state) => !!state.user !== null,
|
||||||
// isLoggedIn: (state) => !!state.token && !!state.user,
|
// isLoggedIn: (state) => !!state.token && !!state.user,
|
||||||
},
|
},
|
||||||
actions: {
|
actions: {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user