Fix logout loop

This commit is contained in:
Tom Moor 2018-05-29 22:18:11 -07:00
parent 4a7f8d3895
commit 57aaea60da
2 changed files with 17 additions and 12 deletions

View File

@ -1,33 +1,34 @@
// @flow // @flow
import * as React from 'react'; import * as React from 'react';
import { Provider } from 'mobx-react'; import { Provider, observer, inject } from 'mobx-react';
import stores from 'stores'; import stores from 'stores';
import AuthStore from 'stores/AuthStore';
import ApiKeysStore from 'stores/ApiKeysStore'; import ApiKeysStore from 'stores/ApiKeysStore';
import UsersStore from 'stores/UsersStore'; import UsersStore from 'stores/UsersStore';
import CollectionsStore from 'stores/CollectionsStore'; import CollectionsStore from 'stores/CollectionsStore';
import IntegrationsStore from 'stores/IntegrationsStore'; import IntegrationsStore from 'stores/IntegrationsStore';
import CacheStore from 'stores/CacheStore'; import CacheStore from 'stores/CacheStore';
import LoadingIndicator from 'components/LoadingIndicator';
type Props = { type Props = {
auth: AuthStore,
children?: React.Node, children?: React.Node,
}; };
let authenticatedStores; let authenticatedStores;
const Auth = ({ children }: Props) => { const Auth = observer(({ auth, children }: Props) => {
if (stores.auth.authenticated) { if (auth.authenticated) {
stores.auth.fetch(); const { user, team } = auth;
// TODO: Show loading state if (!team || !user) {
if (!stores.auth.team || !stores.auth.user) { return <LoadingIndicator />;
return null;
} }
// Only initialize stores once. Kept in global scope because otherwise they // Only initialize stores once. Kept in global scope because otherwise they
// will get overridden on route change // will get overridden on route change
if (!authenticatedStores) { if (!authenticatedStores) {
// Stores for authenticated user // Stores for authenticated user
const { user, team } = stores.auth;
const cache = new CacheStore(user.id); const cache = new CacheStore(user.id);
authenticatedStores = { authenticatedStores = {
integrations: new IntegrationsStore(), integrations: new IntegrationsStore(),
@ -55,8 +56,8 @@ const Auth = ({ children }: Props) => {
return <Provider {...authenticatedStores}>{children}</Provider>; return <Provider {...authenticatedStores}>{children}</Provider>;
} }
stores.auth.logout(); auth.logout();
return null; return null;
}; });
export default Auth; export default inject('auth')(Auth);

View File

@ -58,7 +58,9 @@ class AuthStore {
Cookie.remove('accessToken', { path: '/' }); Cookie.remove('accessToken', { path: '/' });
await localForage.clear(); await localForage.clear();
window.location.href = BASE_URL;
// add a timestamp to force reload from server
window.location.href = `${BASE_URL}?done=${new Date().getTime()}`;
}; };
@action @action
@ -127,6 +129,8 @@ class AuthStore {
// sessions created pre-google auth // sessions created pre-google auth
this.token = Cookie.get('accessToken') || data.token; this.token = Cookie.get('accessToken') || data.token;
if (this.token) setImmediate(() => this.fetch());
autorun(() => { autorun(() => {
try { try {
localStorage.setItem(AUTH_STORE, this.asJson); localStorage.setItem(AUTH_STORE, this.asJson);