Require access token presence in MainContainer auth gate before route entry

Co-Authored-By: Codex <codex@openai.com>
This commit is contained in:
2026-03-08 19:01:03 +08:00
parent 0948a82eb5
commit a8cd590a11
2 changed files with 33 additions and 18 deletions

View File

@@ -56,9 +56,16 @@ export default {
async beforeRouteEnter(to, from, next) { async beforeRouteEnter(to, from, next) {
const loginStore = useLoginStoreInGuard(); const loginStore = useLoginStoreInGuard();
const relativeReturnTo = `${window.location.pathname}${window.location.search}${window.location.hash}`; const relativeReturnTo = `${window.location.pathname}${window.location.search}${window.location.hash}`;
const hasLoginMarker = Boolean(getCookie("isLuciaLoggedIn"));
const hasAccessToken = Boolean(getCookie("luciaToken"));
const hasRefreshToken = Boolean(getCookie("luciaRefreshToken"));
if (!getCookie("isLuciaLoggedIn")) { if (hasLoginMarker && hasAccessToken) {
if (getCookie("luciaRefreshToken")) { next();
return;
}
if (hasRefreshToken) {
try { try {
await loginStore.refreshToken(); await loginStore.refreshToken();
loginStore.setIsLoggedIn(true); loginStore.setIsLoggedIn(true);
@@ -80,9 +87,6 @@ export default {
}, },
}); });
} }
} else {
next();
}
}, },
// Remember, Swal modal handling is called before beforeRouteUpdate // Remember, Swal modal handling is called before beforeRouteUpdate
beforeRouteUpdate(to, from, next) { beforeRouteUpdate(to, from, next) {

View File

@@ -84,14 +84,25 @@ describe("MainContainer beforeRouteEnter", () => {
); );
}); });
it("calls next() when already logged in", async () => { it("calls next() when logged-in marker and access token both exist", async () => {
document.cookie = "isLuciaLoggedIn=true"; document.cookie = "isLuciaLoggedIn=true";
document.cookie = "luciaToken=token";
await callGuard(); await callGuard();
expect(next).toHaveBeenCalled(); expect(next).toHaveBeenCalled();
}); });
it("redirects to login when logged-in marker exists without access token", async () => {
document.cookie = "isLuciaLoggedIn=true";
await callGuard();
expect(next).toHaveBeenCalledWith(
expect.objectContaining({ path: "/login" }),
);
});
it("stores a relative return-to path when redirecting to login", async () => { it("stores a relative return-to path when redirecting to login", async () => {
window.history.replaceState( window.history.replaceState(
{}, {},