Compute refresh token expiry fresh on each sign-in
The expiry date was computed once at store init time and went stale in long-running SPA sessions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -30,7 +30,6 @@ export const useLoginStore = defineStore('loginStore', {
|
|||||||
userData: {},
|
userData: {},
|
||||||
isLoggedIn: false,
|
isLoggedIn: false,
|
||||||
rememberedReturnToUrl: "",
|
rememberedReturnToUrl: "",
|
||||||
expired: (() => { const d = new Date(); d.setMonth(d.getMonth() + 6); return d.getTime(); })(), // 設定 Refresh Token 的到期日為半年後
|
|
||||||
}),
|
}),
|
||||||
actions: {
|
actions: {
|
||||||
/**
|
/**
|
||||||
@@ -51,7 +50,9 @@ export const useLoginStore = defineStore('loginStore', {
|
|||||||
const refresh_token = response.data.refresh_token;
|
const refresh_token = response.data.refresh_token;
|
||||||
// 將 token 儲存在 cookie
|
// 將 token 儲存在 cookie
|
||||||
setCookieWithoutExpiration("luciaToken", accessToken);
|
setCookieWithoutExpiration("luciaToken", accessToken);
|
||||||
setCookie("luciaRefreshToken", refresh_token, Math.ceil((this.expired - Date.now()) / (24 * 60 * 60 * 1000)));
|
const expiryDate = new Date();
|
||||||
|
expiryDate.setMonth(expiryDate.getMonth() + 6);
|
||||||
|
setCookie("luciaRefreshToken", refresh_token, Math.ceil((expiryDate.getTime() - Date.now()) / (24 * 60 * 60 * 1000)));
|
||||||
|
|
||||||
this.isLoggedIn = true;
|
this.isLoggedIn = true;
|
||||||
setCookie("isLuciaLoggedIn", "true");
|
setCookie("isLuciaLoggedIn", "true");
|
||||||
|
|||||||
@@ -249,16 +249,4 @@ describe('loginStore', () => {
|
|||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('expired', () => {
|
|
||||||
it('is approximately 6 months in the future', () => {
|
|
||||||
const now = new Date();
|
|
||||||
const sixMonthsLater = new Date(now);
|
|
||||||
sixMonthsLater.setMonth(sixMonthsLater.getMonth() + 6);
|
|
||||||
|
|
||||||
const expiredDate = new Date(store.expired);
|
|
||||||
// Allow 1 day tolerance
|
|
||||||
const diffMs = Math.abs(expiredDate.getTime() - sixMonthsLater.getTime());
|
|
||||||
expect(diffMs).toBeLessThan(24 * 60 * 60 * 1000);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in New Issue
Block a user