From 922fd2497cf9e259896ab9ea04675fd65a3ec89a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 18 Dec 2017 22:55:14 -0800 Subject: [PATCH] Fixes #480 --- app/components/Auth.js | 58 ++++++++++++++++++++++++++++++++++++++++++ app/index.js | 55 +-------------------------------------- 2 files changed, 59 insertions(+), 54 deletions(-) create mode 100644 app/components/Auth.js diff --git a/app/components/Auth.js b/app/components/Auth.js new file mode 100644 index 000000000..d4a85cbd6 --- /dev/null +++ b/app/components/Auth.js @@ -0,0 +1,58 @@ +// @flow +import React from 'react'; +import { Provider } from 'mobx-react'; +import stores from 'stores'; +import SettingsStore from 'stores/SettingsStore'; +import DocumentsStore from 'stores/DocumentsStore'; +import CollectionsStore from 'stores/CollectionsStore'; +import CacheStore from 'stores/CacheStore'; + +type Props = { + children?: React.Element, +}; + +let authenticatedStores; + +const Auth = ({ children }: Props) => { + if (stores.auth.authenticated && stores.auth.team && stores.auth.user) { + // Only initialize stores once. Kept in global scope because otherwise they + // will get overridden on route change + if (!authenticatedStores) { + // Stores for authenticated user + const { user, team } = stores.auth; + const cache = new CacheStore(user.id); + authenticatedStores = { + settings: new SettingsStore(), + documents: new DocumentsStore({ + ui: stores.ui, + cache, + }), + collections: new CollectionsStore({ + ui: stores.ui, + teamId: team.id, + cache, + }), + }; + + if (window.Bugsnag) { + Bugsnag.user = { + id: user.id, + name: user.name, + teamId: team.id, + team: team.name, + }; + } + + stores.auth.fetch(); + authenticatedStores.collections.fetchAll(); + } + + return {children}; + } + + stores.auth.logout(); + window.location.href = BASE_URL; + return null; +}; + +export default Auth; diff --git a/app/index.js b/app/index.js index 76f7f71f8..66c04691f 100644 --- a/app/index.js +++ b/app/index.js @@ -10,10 +10,6 @@ import { } from 'react-router-dom'; import stores from 'stores'; -import SettingsStore from 'stores/SettingsStore'; -import DocumentsStore from 'stores/DocumentsStore'; -import CollectionsStore from 'stores/CollectionsStore'; -import CacheStore from 'stores/CacheStore'; import globalStyles from 'shared/styles/globals'; import 'shared/styles/prism.css'; @@ -33,6 +29,7 @@ import Error404 from 'scenes/Error404'; import ErrorBoundary from 'components/ErrorBoundary'; import ScrollToTop from 'components/ScrollToTop'; import Layout from 'components/Layout'; +import Auth from 'components/Auth'; import RouteSidebarHidden from 'components/RouteSidebarHidden'; import { matchDocumentSlug } from 'utils/routeHelpers'; @@ -42,53 +39,6 @@ if (__DEV__) { DevTools = require('mobx-react-devtools').default; // eslint-disable-line global-require } -let authenticatedStores; - -type AuthProps = { - children?: React.Element, -}; - -const Auth = ({ children }: AuthProps) => { - if (stores.auth.authenticated && stores.auth.team && stores.auth.user) { - // Only initialize stores once. Kept in global scope because otherwise they - // will get overridden on route change - if (!authenticatedStores) { - // Stores for authenticated user - const { user, team } = stores.auth; - const cache = new CacheStore(user.id); - authenticatedStores = { - settings: new SettingsStore(), - documents: new DocumentsStore({ - ui: stores.ui, - cache, - }), - collections: new CollectionsStore({ - ui: stores.ui, - teamId: team.id, - cache, - }), - }; - - if (window.Bugsnag) { - Bugsnag.user = { - id: user.id, - name: user.name, - teamId: team.id, - team: team.name, - }; - } - - stores.auth.fetch(); - authenticatedStores.collections.fetchAll(); - } - - return {children}; - } - - stores.auth.logout(); - window.location.href = BASE_URL; -}; - const notFoundSearch = () => ; const DocumentNew = () => ; const RedirectDocument = ({ match }: { match: Object }) => ( @@ -105,7 +55,6 @@ render( - @@ -172,5 +121,3 @@ render( , document.getElementById('root') ); - -window.authenticatedStores = authenticatedStores;