From dac6ba5900f645ae26624752f564b470664ceb4a Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sat, 18 Apr 2020 15:31:55 +0300 Subject: [PATCH] Login: automatically log out on expired token. This is a bit of a workaround. The problem is that we cache auth tokens, but that auth tokens can expire. Thins means that we could have a stale auth token after coming back to the app after a long time, so we need to fetch a new one. Logging out is a bit of a nuclear option, but since this is a rare scenario, it can do for now. --- src/LoginGate.tsx | 12 +++++++++--- src/store/actions.ts | 4 ++-- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/LoginGate.tsx b/src/LoginGate.tsx index 9758138..4935b7d 100644 --- a/src/LoginGate.tsx +++ b/src/LoginGate.tsx @@ -12,7 +12,7 @@ import LoginForm from './components/LoginForm'; import EncryptionLoginForm from './components/EncryptionLoginForm'; import { store, StoreState, CredentialsDataRemote } from './store'; -import { deriveKey, fetchCredentials, fetchUserInfo } from './store/actions'; +import { deriveKey, fetchCredentials, fetchUserInfo, logout } from './store/actions'; import * as EteSync from 'etesync'; import * as C from './constants'; @@ -35,8 +35,14 @@ function EncryptionPart(props: { credentials: CredentialsDataRemote }) { setUserInfo(fetchedUserInfo.payload); }).catch((e: Error) => { // Do nothing. - if ((e instanceof EteSync.HTTPError) && (e.status !== 404)) { - setError(e); + if (e instanceof EteSync.HTTPError) { + if (e.status === 404) { + // Do nothing + } else if (e.status === 401) { + store.dispatch(logout(credentials)); + } else { + setError(e); + } } }).finally(() => { setFetched(true); diff --git a/src/store/actions.ts b/src/store/actions.ts index 72be410..0730ec0 100644 --- a/src/store/actions.ts +++ b/src/store/actions.ts @@ -6,7 +6,7 @@ import { Action, createAction, createActions } from 'redux-actions'; import * as EteSync from 'etesync'; import { UserInfo } from 'etesync'; -import { CredentialsData, EntriesData, SettingsType } from './'; +import { CredentialsData, CredentialsDataRemote, EntriesData, SettingsType } from './'; export const { fetchCredentials } = createActions({ FETCH_CREDENTIALS: (username: string, password: string, server: string) => { @@ -34,7 +34,7 @@ export const { fetchCredentials } = createActions({ export const logout = createAction( 'LOGOUT', - (etesync: CredentialsData) => { + (etesync: CredentialsDataRemote) => { (async () => { const authenticator = new EteSync.Authenticator(etesync.serviceApiUrl); try {