diff --git a/js/components/ascribe_routes/proxy_handler.js b/js/components/ascribe_routes/proxy_handler.js index 55987ee5..225420b0 100644 --- a/js/components/ascribe_routes/proxy_handler.js +++ b/js/components/ascribe_routes/proxy_handler.js @@ -34,8 +34,8 @@ export function AuthRedirect({to, when}) { // // So if when === 'loggedIn', we're checking if the user is logged in (and // vice versa) - const exprToValidate = when === 'loggedIn' ? currentUser && currentUser.email - : currentUser && !currentUser.email; + const isLoggedIn = currentUser && Object.keys(currentUser).length && currentUser.email; + const exprToValidate = when === 'loggedIn' ? isLoggedIn : !isLoggedIn; // and redirect if `true`. if (exprToValidate) { @@ -80,7 +80,11 @@ export function ProxyHandler(...redirectFunctions) { displayName: 'ProxyHandler', propTypes: { - currentUser: object, + // Provided from AscribeApp + currentUser: React.PropTypes.object, + whitelabel: React.PropTypes.object, + + // Provided from router location: object }, @@ -88,10 +92,18 @@ export function ProxyHandler(...redirectFunctions) { // to use the `Lifecycle` widget in further down nested components mixins: [RouteContext], - componentDidUpdate() { - const { currentUser, location: { query } } = this.props; + componentDidMount() { + this.evaluateRedirectFunctions(); + }, - if (!UserStore.isLoading()) { + componentWillReceiveProps(nextProps) { + this.evaluateRedirectFunctions(nextProps); + }, + + evaluateRedirectFunctions(props = this.props) { + const { currentUser, location: { query } } = props; + + if (UserStore.hasLoaded() && !UserStore.isLoading()) { for (let i = 0; i < redirectFunctions.length; i++) { // if a redirectFunction redirects the user, // it should return `true` and therefore @@ -106,7 +118,7 @@ export function ProxyHandler(...redirectFunctions) { render() { return ( - + ); } }); diff --git a/js/stores/user_store.js b/js/stores/user_store.js index f07e56e7..41a9c962 100644 --- a/js/stores/user_store.js +++ b/js/stores/user_store.js @@ -12,12 +12,16 @@ class UserStore { constructor() { this.currentUser = {}; this.userMeta = { + hasLoaded: false, invalidateCache: false, err: null }; this.bindActions(UserActions); this.registerAsync(UserSource); + this.exportPublicMethods({ + hasLoaded: this.hasLoaded.bind(this) + }); } onFetchCurrentUser(invalidateCache) { @@ -28,7 +32,8 @@ class UserStore { } } - onSuccessFetchCurrentUser({users: [user = {}]}) { + onSuccessFetchCurrentUser({ users: [ user = {} ] = [] }) { + this.userMeta.hasLoaded = true; this.userMeta.invalidateCache = false; this.userMeta.err = null; @@ -50,6 +55,10 @@ class UserStore { altWhitelabel.recycle(); altUser.recycle(); altThirdParty.recycle(); + + // Since we've just logged out, we can set this store's + // hasLoaded flag back to true as there is no current user. + this.userMeta.hasLoaded = true; }); } @@ -59,8 +68,13 @@ class UserStore { onErrorCurrentUser(err) { console.logGlobal(err); + this.userMeta.hasLoaded = true; this.userMeta.err = err; } + + hasLoaded() { + return this.userMeta.hasLoaded; + } } export default altUser.createStore(UserStore, 'UserStore');