fix: revalidate user token with no refresh page (#1842)

This commit is contained in:
Himself65
2023-04-07 17:51:51 -05:00
committed by GitHub
parent e50bf9fbfe
commit 20e56cc474
6 changed files with 83 additions and 41 deletions

View File

@@ -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,
});
}

View File

@@ -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();
});
});