-
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
Conversation
AndrewGable
left a comment
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 tests well for me across clients 👍
src/Expensify.js
Outdated
|
|
||
| this.recordCurrentRoute = this.recordCurrentRoute.bind(this); | ||
|
|
||
| this.state = {}; |
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.
Why is this necessary?
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.
When this component first renders it has an undefined state. After withIon mounts (i.e. post render cycle of the Expensify component) only then do we look at the prefillWithKey and set it to this components state. Before that it had no state at all.
Which is why we needed to check for this.state && this.state.redirectTo before. If we know this is always at least going to be {} then we can safely access this.state.redirectTo.
src/Expensify.js
Outdated
| } | ||
|
|
||
| render() { | ||
| const redirectTo = this.state.redirectTo || (!this.state.authToken && '/signin') || '/'; |
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 think if we are going to add this, you should add a comment with some of the info you added in the description to explain why this is necessary
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.
Yep that's a good call.
|
Looks like this PR also fixes #235, I tested by:
|
|
Can't reproduce #230 either on this branch 🎉 |
|
Ok one minor thing I see on this PR, if you are on /36 and refresh, it takes you to / then to a different report (e.g. 17). I don't think it's a blocker, but just a small regression from |
|
Updated. And the bug you found should be fixed now 🤞 |
AndrewGable
left a comment
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.
Fixed all little buggies I saw on the previous commits 👍
|
I'm still seeing #239 (comment) on this branch, if I signout while on a reportID and sign in again, I'm taken to the first reportID in the list instead of the one in the exitTo. Can you confirm? |
|
After double checking, I just realized that the |
| @@ -1,4 +1,6 @@ | |||
| import React, {Component} from 'react'; | |||
| import {View} from 'react-native'; | |||
| import get from 'lodash.get'; | |||
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 grepping
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 want to propose something even crazier...
Let's just ditch underscore already and use lodash for everything.
What does underscore have that lodash doesn'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:
- There is no problem using underscore
- Lodash handles some functionality slightly differently than underscore, and if an engineer isn't aware of that, it can lead to difficult to find bugs (this was more of a historical issue and it's possible the APIs are more aligned today than they have been in the past)
- Some underscore functionality we use doesn't exist in lodash (eg.
findWhere()indexBy()pluck()) - Lodash methods can be imported individually for functionality that underscore doesn't have (but the vice versa is not true)
- Lodash is 24kb vs underscore 6kb and includes a lot of stuff that would never be used
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 😛
| .then(redirectToSignIn); | ||
| } | ||
| return setSuccessfulSignInData(response, command.exitTo); | ||
| return setSuccessfulSignInData(response, data.exitTo); |
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 wonder how much this tripped me up
tgolen
left a comment
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.
Would love to talk about possibly leaning on React Router more for all of our navigation needs
Yes, I totally agree and would love to explore possible solutions and paths for this. Can you make a GH to discuss? One thing I find difficult about Router is that its primary use is with UI components, and that makes it hard to interact with it inside of just a normal JS lib. Hopefully we can figure out some good solutions for that.
| // 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) |
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 can be simplified a little bit to pass back just the authToken:
Ion.get(IONKEYS.SESSION, 'authToken', false)
Fixes #229
Fixes #235
cc @tgolen @AndrewGable in case you have a better idea about how to do this... but here's my current reasoning...
Presently, when the app inits we always navigate to the homepage by default because there is little to stop up from navigating there and
redirectTois alwaysundefinedon the very first render (technically all state isundefinedon the first render).Since the presence of an
authTokenshould tell us if we are logged in or not I enforced that when the app inits (or we are redirecting to'/'- which we don't ever do) then we will check for the presence of anauthTokenand redirect to the root if we have one. If we don't have one then we'll redirect to the/signin. And when we have aredirectTothen we'll just use that.Lmk if I am holding this completely wrong here...
Tests:
authTokenhttp://localhost:8082/bonus: post-prototype thoughts (for my reference, but feel free to read on)
Would love to talk about possibly leaning on React Router more for all of our navigation needs and making site navigation something that
Iondoesn't really need much awareness of. There's the HOCwithRouterthat can be used just about anywhere to find out where we are or redirect us to someplace else.