feat: HttpOnly Cookie 登入狀態初始化

This commit is contained in:
skytek_xinliang
2026-08-31 09:26:47 +08:00
parent db089ce267
commit 28cda7be1c
5 changed files with 60 additions and 7 deletions
+4 -2
View File
@@ -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 },