-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Check authToken and always redirect somewhere on init #239
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
6de7009
Check authToken and always redirect somewhere on init
marcaaron d379717
add comment
marcaaron 7541f7e
add a generic loader to provide time to load the authToken
marcaaron f4017c9
woops
marcaaron a770a75
undo these weird changes
marcaaron 9da16d5
fix exitTo
marcaaron File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,6 @@ | ||
| import React, {Component} from 'react'; | ||
| import {View} from 'react-native'; | ||
| import get from 'lodash.get'; | ||
|
|
||
| // import {Beforeunload} from 'react-beforeunload'; | ||
| import SignInPage from './page/SignInPage'; | ||
|
|
@@ -8,6 +10,8 @@ import * as ActiveClientManager from './lib/ActiveClientManager'; | |
| import {verifyAuthToken} from './lib/actions/ActionsSession'; | ||
| import IONKEYS from './IONKEYS'; | ||
| import WithIon from './components/WithIon'; | ||
| import styles from './style/StyleSheet'; | ||
|
|
||
| import { | ||
| Route, | ||
| Router, | ||
|
|
@@ -23,6 +27,22 @@ class Expensify extends Component { | |
| super(props); | ||
|
|
||
| this.recordCurrentRoute = this.recordCurrentRoute.bind(this); | ||
|
|
||
| this.state = { | ||
| loading: true, | ||
| authenticated: false, | ||
| }; | ||
| } | ||
|
|
||
| componentDidMount() { | ||
| // We need to delay initializing the main app so we can check for an authToken and | ||
| // redirect to the signin page if we do not have one. Otherwise when the app inits | ||
| // we will fall through to the homepage and the user will see a brief flash of the main | ||
| // app experience. | ||
| Ion.get(IONKEYS.SESSION) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be simplified a little bit to pass back just the authToken: |
||
| .then((response) => { | ||
| this.setState({loading: false, authenticated: Boolean(get(response, 'authToken', false))}); | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -35,13 +55,23 @@ class Expensify extends Component { | |
| } | ||
|
|
||
| render() { | ||
| if (this.state.loading) { | ||
| return ( | ||
| <View style={styles.genericView} /> | ||
| ); | ||
| } | ||
|
|
||
| // We can only have a redirectTo if this is not the initial render so if we have one we'll | ||
| // always navigate to it. If we are not authenticated by this point then we'll force navigate to sign in. | ||
| const redirectTo = this.state.redirectTo || (!this.state.authenticated && '/signin'); | ||
|
|
||
| return ( | ||
|
|
||
| // TODO: Mobile does not support Beforeunload | ||
| // <Beforeunload onBeforeunload={ActiveClientManager.removeClient}> | ||
| <Router> | ||
| {/* If there is ever a property for redirecting, we do the redirect here */} | ||
| {this.state && this.state.redirectTo && <Redirect to={this.state.redirectTo} />} | ||
| {redirectTo && <Redirect to={redirectTo} />} | ||
| <Route path="*" render={this.recordCurrentRoute} /> | ||
|
|
||
| <Switch> | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -172,7 +172,7 @@ function request(command, data, type = 'post') { | |
| }) | ||
| .then(redirectToSignIn); | ||
| } | ||
| return setSuccessfulSignInData(response, command.exitTo); | ||
| return setSuccessfulSignInData(response, data.exitTo); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wonder how much this tripped me up |
||
| }) | ||
| .then((response) => { | ||
| // If Expensify login, it's the users first time signing in and we need to | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was imported somewhere else as
lodashGet, maybe we should try to import these using a standard naming convention, for easy greppingThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I want to propose something even crazier...
Let's just ditch
underscorealready and uselodashfor everything.What does
underscorehave thatlodashdoesn't?https://lodash.com/docs/4.17.15#map
https://lodash.com/docs/4.17.15#chain
https://lodash.com/docs/4.17.15#forEach
https://lodash.com/docs/4.17.15#size
https://lodash.com/docs/4.17.15#find
https://lodash.com/docs/4.17.15#uniqueId
the list goes on and on...
I mean heck it even has
_.template(but where we're going we don't need templates)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've definitely thought of that in the past as well, and I keep coming back to this:
findWhere()indexBy()pluck())There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, it was worth a shot 😛