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 4 years ago
parent 84f7a11bbc
commit dac6ba5900

@ -12,7 +12,7 @@ import LoginForm from './components/LoginForm';
import EncryptionLoginForm from './components/EncryptionLoginForm'; import EncryptionLoginForm from './components/EncryptionLoginForm';
import { store, StoreState, CredentialsDataRemote } from './store'; 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 EteSync from 'etesync';
import * as C from './constants'; import * as C from './constants';
@ -35,9 +35,15 @@ function EncryptionPart(props: { credentials: CredentialsDataRemote }) {
setUserInfo(fetchedUserInfo.payload); setUserInfo(fetchedUserInfo.payload);
}).catch((e: Error) => { }).catch((e: Error) => {
// Do nothing. // Do nothing.
if ((e instanceof EteSync.HTTPError) && (e.status !== 404)) { if (e instanceof EteSync.HTTPError) {
if (e.status === 404) {
// Do nothing
} else if (e.status === 401) {
store.dispatch(logout(credentials));
} else {
setError(e); setError(e);
} }
}
}).finally(() => { }).finally(() => {
setFetched(true); setFetched(true);
}); });

@ -6,7 +6,7 @@ import { Action, createAction, createActions } from 'redux-actions';
import * as EteSync from 'etesync'; import * as EteSync from 'etesync';
import { UserInfo } from 'etesync'; import { UserInfo } from 'etesync';
import { CredentialsData, EntriesData, SettingsType } from './'; import { CredentialsData, CredentialsDataRemote, EntriesData, SettingsType } from './';
export const { fetchCredentials } = createActions({ export const { fetchCredentials } = createActions({
FETCH_CREDENTIALS: (username: string, password: string, server: string) => { FETCH_CREDENTIALS: (username: string, password: string, server: string) => {
@ -34,7 +34,7 @@ export const { fetchCredentials } = createActions({
export const logout = createAction( export const logout = createAction(
'LOGOUT', 'LOGOUT',
(etesync: CredentialsData) => { (etesync: CredentialsDataRemote) => {
(async () => { (async () => {
const authenticator = new EteSync.Authenticator(etesync.serviceApiUrl); const authenticator = new EteSync.Authenticator(etesync.serviceApiUrl);
try { try {

Loading…
Cancel
Save