From 3884f139aad158f8a7434909753f7d99faa51a44 Mon Sep 17 00:00:00 2001 From: "MJM_2025_05\\polly" Date: Tue, 26 Aug 2025 17:53:09 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=81=A2=E5=BE=A9=20router=20redirect?= =?UTF-8?q?=20=E6=8C=87=E5=90=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/router/index-84b479b.js | Bin 0 -> 7062 bytes src/router/index.js | 43 ++++++++++++++---------------------- 2 files changed, 16 insertions(+), 27 deletions(-) create mode 100644 src/router/index-84b479b.js diff --git a/src/router/index-84b479b.js b/src/router/index-84b479b.js new file mode 100644 index 0000000000000000000000000000000000000000..01b98c515eae0513f5caa4e271f7d42d8be7d32c GIT binary patch literal 7062 zcmeHMTW=dh6h6;L{D-X{DkH%zh45JC~CC@tzkRYi{N*rv`p_>v3YzuxkFXLdY0 zv$L1P<_)dv_3q4^%QxRSGjsgu<4msQMi#P^wIuR4K12K-%K*<~IY-Mze=X(9pzST% zFXbonT*(yo8SZP9^njjQe6ApA0r`panwzelO*v zH7{U{_4rv}kp@s-xmMUdw~pI#x`| z=+KK>)pw^ptV%sI`0mD~1(NL=ew4Xdx{M&!O_!4pYUQjAAw5@r5^q)$IflkM7WKg~ zj!tk*JTLos3W}HAe+Wo+c{R280)=1eq{}H`6y%>**4S| zpTkG4DY1(C z6smeMc~!CZkZ4R4i5qZ(?Bu*pfDYHP9QefM+ULgO$M26)v(8M;8htt>dIMxT`m<5A zo`&GlWVE4Z_dPbF@ZPqsXtyN84y(=M!{WQbD#?A`Q?Y&Q_%>YIQ9Oag^)?g1ruM_4 zH`h$pC5{JJVVM=0s*4r3s4vFnGuwd1N zb(AY$%E-uo?;rTY{PkDg*yz}({ZV>T*x1c?yI0-fRQp0t+FXy^8GMP;`XwZ?%EME_ zDfq;5LmzjZJ90cFcw%jdH7Hv*;cxQ(9xd*?>~_BNll6&(;+W!_r(>R>oo?cj>?E_t z5L)eCkZmJ3s-EMtV|cXPlUN0(VwiKd$hdLn3<` z3)wQ#HumK{j88mKKWLmy^l71OTz|=zl|C8mWX}>ksohB8-k(zKKENVFoi}>!oNNEz zihh*F8qPal6;?Y~)5!37U(6*vFUuQ1-Ws!a4t;;(w?Tp)P~x*1eW`jj1Ogj)OOMQa zr|8f5nC`e!r_ac5tNpREVmKJeA-g_6Gcl*7M$#GH`1L^Y=Vy5NQ~RQXu-4Vgbz^52?~j3TqHFBE zqD1Z;%hy28VNd+K>8pC*Rlen7TFfRhO*qfJidC^uRU0fCp-(o4%y$)Q6MLy7&on!$IazF-X@rj zIWm#U(|!12?FjSR|IuW!Oc)OqukyTgi>57D;%30D>ttU{Y8ct>+0Qz$v2_+z-#b|* g9{iZH@!}$f`I&nZ_a7VAw=4Bvj>z!;J2BG#1?K=Jj{pDw literal 0 HcmV?d00001 diff --git a/src/router/index.js b/src/router/index.js index 290953e..49db595 100644 --- a/src/router/index.js +++ b/src/router/index.js @@ -79,49 +79,38 @@ const router = createRouter({ ], }); -router.beforeEach((to, from, next) => { +router.beforeEach(async (to, from, next) => { console.log("route", to, location, document.cookie); + // redirect to login page if not logged in and trying to access a restricted page const publicPages = ["/login", "/"]; - const requiresAuth = !publicPages.includes(to.path); - + const authRequired = !publicPages.includes(to.path); const auth = useUserInfoStore(); const token = useGetCookie("JWT-Authorization"); const user_name = useGetCookie("user_name"); - // 處理 /logout if (to.path === "/logout") { - // 清除 cookie(建議補 Path 與 SameSite) - document.cookie = "JWT-Authorization=; Max-Age=0; Path=/"; - document.cookie = "user_name=; Max-Age=0; Path=/"; - - // 清除狀態 + document.cookie = "JWT-Authorization=; Max-Age=0"; + document.cookie = "user_name=; Max-Age=0"; auth.user.token = ""; auth.user.user_name = ""; localStorage.removeItem("EmpowerBuilding"); - - // 直接導回登入(避免 reload 與 next() 交疊) - return next({ path: "/login", replace: true }); + window.location.reload(); + next({ path: "/login" }); } - // 未登入:擋住受保護頁 - if (requiresAuth && !token) { + if ((authRequired && !token) || to.path === "/") { auth.user.token = ""; - return next({ path: "/login", replace: true }); - } - - // 進公開頁(例如 /login):清掉使用者狀態(若你想保留語系就不要清) - if (!requiresAuth) { - document.cookie = "JWT-Authorization=; Max-Age=0; Path=/"; - document.cookie = "user_name=; Max-Age=0; Path=/"; + next({ path: "/login" }); + } else if (!authRequired) { + document.cookie = "JWT-Authorization=; Max-Age=0"; + document.cookie = "user_name=; Max-Age=0"; auth.user.token = ""; auth.user.user_name = ""; - return next(); + } else { + auth.user.token = token; + auth.user.user_name = user_name; } - - // 受保護頁且有 token:同步 Pinia 狀態 - auth.user.token = token; - auth.user.user_name = user_name; - return next(); + next(); }); export default router;