feat: HttpOnly Cookie 登入狀態初始化
This commit is contained in:
@@ -78,8 +78,9 @@ const router = createRouter({
|
||||
});
|
||||
|
||||
// 簡單的導航守衛:只有標記需要登入的頁面會導向登入頁
|
||||
router.beforeEach((to) => {
|
||||
router.beforeEach(async (to) => {
|
||||
const store = useAppStore();
|
||||
await store.initializeAuthentication();
|
||||
|
||||
if (to.meta.requiresAuth && !store.isLoggedIn) {
|
||||
return {
|
||||
@@ -99,9 +100,10 @@ if (typeof window !== "undefined") {
|
||||
window.addEventListener("auth-expired", () => {
|
||||
const store = useAppStore();
|
||||
const currentRoute = router.currentRoute.value;
|
||||
const wasLoggedIn = store.isLoggedIn;
|
||||
store.clearAuthentication();
|
||||
|
||||
if (currentRoute.name !== "login") {
|
||||
if (wasLoggedIn && currentRoute.name !== "login") {
|
||||
void router.replace({
|
||||
name: "login",
|
||||
query: { redirect: currentRoute.fullPath },
|
||||
|
||||
Reference in New Issue
Block a user