mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-02-12 04:18:54 +00:00
fix: revalidate user token with no refresh page (#1842)
This commit is contained in:
@@ -4,6 +4,7 @@ import {
|
||||
isExpired,
|
||||
parseIdToken,
|
||||
setLoginStorage,
|
||||
storageChangeSlot,
|
||||
} from '@affine/workspace/affine/login';
|
||||
import useSWR from 'swr';
|
||||
|
||||
@@ -21,10 +22,7 @@ const revalidate = async () => {
|
||||
const response = await affineAuth.refreshToken(storage);
|
||||
if (response) {
|
||||
setLoginStorage(response);
|
||||
|
||||
// todo: need to notify the app that the token has been refreshed
|
||||
// this is a hack to force a reload
|
||||
window.location.reload();
|
||||
storageChangeSlot.emit();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -38,5 +36,8 @@ export function useAffineRefreshAuthToken(
|
||||
useSWR('autoRefreshToken', {
|
||||
fetcher: revalidate,
|
||||
refreshInterval,
|
||||
revalidateOnFocus: true,
|
||||
revalidateOnReconnect: true,
|
||||
revalidateOnMount: true,
|
||||
});
|
||||
}
|
||||
|
||||
@@ -40,10 +40,11 @@ describe('AFFiNE workspace', () => {
|
||||
// but refresh is still valid
|
||||
refresh: data.refresh,
|
||||
});
|
||||
renderHook(() => useAffineRefreshAuthToken(1));
|
||||
const hook = renderHook(() => useAffineRefreshAuthToken(1));
|
||||
await new Promise(resolve => setTimeout(resolve, 3000));
|
||||
const userData = parseIdToken(getLoginStorage()?.token as string);
|
||||
expect(userData).not.toBeNull();
|
||||
expect(isExpired(userData)).toBe(false);
|
||||
hook.unmount();
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user