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.
master
Tom Hacohen 5 years ago
parent 84f7a11bbc
commit dac6ba5900

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

@ -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 {

Loading…
Cancel
Save