fix session expired message shown during login flow (#1049)

This commit is contained in:
Abhinav Kumar 2023-04-14 09:05:44 +05:30 committed by GitHub
commit fc3114d2b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 15 deletions

View file

@ -109,6 +109,7 @@ import { checkConnectivity } from 'utils/common';
import { SYNC_INTERVAL_IN_MICROSECONDS } from 'constants/gallery'; import { SYNC_INTERVAL_IN_MICROSECONDS } from 'constants/gallery';
import ElectronService from 'services/electron/common'; import ElectronService from 'services/electron/common';
import uploadManager from 'services/upload/uploadManager'; import uploadManager from 'services/upload/uploadManager';
import { getToken } from 'utils/common/key';
export const DeadCenter = styled('div')` export const DeadCenter = styled('div')`
flex: 1; flex: 1;
@ -188,7 +189,8 @@ export default function Gallery() {
const [searchResultSummary, setSetSearchResultSummary] = const [searchResultSummary, setSetSearchResultSummary] =
useState<SearchResultSummary>(null); useState<SearchResultSummary>(null);
const syncInProgress = useRef(true); const syncInProgress = useRef(true);
const resync = useRef(false); const syncInterval = useRef<NodeJS.Timeout>();
const resync = useRef<{ force: boolean; silent: boolean }>();
const [deletedFileIds, setDeletedFileIds] = useState<Set<number>>( const [deletedFileIds, setDeletedFileIds] = useState<Set<number>>(
new Set<number>() new Set<number>()
); );
@ -265,14 +267,18 @@ export default function Gallery() {
setIsFirstLoad(false); setIsFirstLoad(false);
setJustSignedUp(false); setJustSignedUp(false);
setIsFirstFetch(false); setIsFirstFetch(false);
setInterval(() => { syncInterval.current = setInterval(() => {
syncWithRemote(false, true); syncWithRemote(false, true);
}, SYNC_INTERVAL_IN_MICROSECONDS); }, SYNC_INTERVAL_IN_MICROSECONDS);
ElectronService.registerForegroundEventListener(() => ElectronService.registerForegroundEventListener(() => {
syncWithRemote(false, true) syncWithRemote(false, true);
); });
}; };
main(); main();
return () => {
clearInterval(syncInterval.current);
ElectronService.registerForegroundEventListener(() => {});
};
}, []); }, []);
useEffect(() => { useEffect(() => {
@ -341,13 +347,18 @@ export default function Gallery() {
const syncWithRemote = async (force = false, silent = false) => { const syncWithRemote = async (force = false, silent = false) => {
if (syncInProgress.current && !force) { if (syncInProgress.current && !force) {
resync.current = true; resync.current = { force, silent };
return; return;
} }
syncInProgress.current = true; syncInProgress.current = true;
try { try {
checkConnectivity(); checkConnectivity();
if (!(await isTokenValid())) { const token = getToken();
if (!token) {
return;
}
const tokenValid = await isTokenValid(token);
if (!tokenValid) {
throw new Error(ServerErrorCodes.SESSION_EXPIRED); throw new Error(ServerErrorCodes.SESSION_EXPIRED);
} }
!silent && startLoading(); !silent && startLoading();
@ -375,8 +386,9 @@ export default function Gallery() {
} }
syncInProgress.current = false; syncInProgress.current = false;
if (resync.current) { if (resync.current) {
resync.current = false; const { force, silent } = resync.current;
setTimeout(() => syncWithRemote(), 0); setTimeout(() => syncWithRemote(force, silent), 0);
resync.current = null;
} }
}; };

View file

@ -167,16 +167,13 @@ export const clearFiles = async () => {
await localForage.clear(); await localForage.clear();
}; };
export const isTokenValid = async () => { export const isTokenValid = async (token: string) => {
try { try {
if (!getToken()) {
return false;
}
const resp = await HTTPService.get( const resp = await HTTPService.get(
`${ENDPOINT}/users/session-validity/v2`, `${ENDPOINT}/users/session-validity/v2`,
null, null,
{ {
'X-Auth-Token': getToken(), 'X-Auth-Token': token,
} }
); );
try { try {
@ -186,7 +183,7 @@ export const isTokenValid = async () => {
if (!resp.data['hasSetKeys']) { if (!resp.data['hasSetKeys']) {
try { try {
await putAttributes( await putAttributes(
getToken(), token,
getData(LS_KEYS.ORIGINAL_KEY_ATTRIBUTES) getData(LS_KEYS.ORIGINAL_KEY_ATTRIBUTES)
); );
} catch (e) { } catch (e) {