fix(R61): 增加 session hint cookie 消除 bootstrap refresh 401
这个提交包含在:
@@ -58,6 +58,17 @@ function isWindowAvailable() {
|
||||
return typeof window !== "undefined";
|
||||
}
|
||||
|
||||
function hasSessionHint() {
|
||||
return typeof document !== "undefined" && document.cookie.includes("has_session=1");
|
||||
}
|
||||
|
||||
function clearSessionHint() {
|
||||
if (typeof document === "undefined") {
|
||||
return;
|
||||
}
|
||||
document.cookie = "has_session=; path=/; max-age=0";
|
||||
}
|
||||
|
||||
function readMustChangePasswordFromStorage() {
|
||||
return isWindowAvailable() && window.localStorage.getItem(MUST_CHANGE_PASSWORD_STORAGE_KEY) === "1";
|
||||
}
|
||||
@@ -168,7 +179,7 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const clearSession = useCallback(() => {
|
||||
const markUnauthenticatedReady = useCallback(() => {
|
||||
sessionGenerationRef.current += 1;
|
||||
bootstrapPromiseRef.current = null;
|
||||
refreshRequestRef.current = null;
|
||||
@@ -189,6 +200,12 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
}));
|
||||
}, [clearProactiveRefreshTimer]);
|
||||
|
||||
const clearSession = useCallback(() => {
|
||||
clearSessionHint();
|
||||
markUnauthenticatedReady();
|
||||
void clearPwaRuntimeCaches();
|
||||
}, [markUnauthenticatedReady]);
|
||||
|
||||
const commitAuthenticatedUser = useCallback(
|
||||
(accessToken: string, me: MeResponse, mustChangePassword: boolean) => {
|
||||
const permissions = resolvePermissionsForRoles(me.roleCodes ?? []);
|
||||
@@ -301,6 +318,11 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasSessionHint() && (!isWindowAvailable() || window.navigator.onLine !== false)) {
|
||||
markUnauthenticatedReady();
|
||||
return;
|
||||
}
|
||||
|
||||
if (!bootstrapPromiseRef.current) {
|
||||
bootstrapPromiseRef.current = (async () => {
|
||||
try {
|
||||
@@ -323,7 +345,7 @@ export function AuthProvider({ children }: PropsWithChildren) {
|
||||
}
|
||||
|
||||
await bootstrapPromiseRef.current;
|
||||
}, [activateOfflineSnapshot, clearSession, runRefresh, scheduleProactiveRefresh]);
|
||||
}, [activateOfflineSnapshot, clearSession, markUnauthenticatedReady, runRefresh, scheduleProactiveRefresh]);
|
||||
|
||||
const login = useCallback(
|
||||
async (identifier: string, password: string) => {
|
||||
|
||||
在新工单中引用
屏蔽一个用户