Skip to content

Conversation

@marcaaron
Copy link
Contributor

@marcaaron marcaaron commented Aug 19, 2020

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 redirectTo is always undefined on the very first render (technically all state is undefined on the first render).

Since the presence of an authToken should 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 an authToken and 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 a redirectTo then we'll just use that.

Lmk if I am holding this completely wrong here...

Tests:

  1. Start out logged out with no authToken
  2. Navigate to the site root by entering the url directly into the browser http://localhost:8082/
  3. Verify the homepage does not momentarily flash
  4. Verify that you are able to log in and get redirected to the homepage
  5. Enter the site root into the url browser bar again
  6. Verify that you are still logged in
  7. Log out
  8. Verify you are back on the sign in page
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 Ion doesn't really need much awareness of. There's the HOC withRouter that can be used just about anywhere to find out where we are or redirect us to someplace else.

@marcaaron marcaaron self-assigned this Aug 19, 2020
@marcaaron marcaaron requested a review from AndrewGable August 19, 2020 01:15
Copy link
Contributor

@AndrewGable AndrewGable left a 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 = {};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this necessary?

Copy link
Contributor Author

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') || '/';
Copy link
Contributor

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

Copy link
Contributor Author

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.

@Jag96
Copy link
Contributor

Jag96 commented Aug 19, 2020

Looks like this PR also fixes #235, I tested by:

  1. Merging master into this branch
  2. Deleting the Chat app on my Android emu
  3. Running the app on this branch
  4. Confirming I can sign in (on master you can't)

@Jag96
Copy link
Contributor

Jag96 commented Aug 19, 2020

Can't reproduce #230 either on this branch 🎉

@AndrewGable
Copy link
Contributor

AndrewGable commented Aug 19, 2020

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 master

@AndrewGable
Copy link
Contributor

This bug also happens if you click sign out on /36, you sign back into /17, I believe the expected behavior there is to go back to /36 as the URL is set to /signin/exitTo/36

This was added in #155 and #179

@marcaaron
Copy link
Contributor Author

Updated. And the bug you found should be fixed now 🤞

Copy link
Contributor

@AndrewGable AndrewGable left a 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 👍

@Jag96
Copy link
Contributor

Jag96 commented Aug 19, 2020

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?

@AndrewGable
Copy link
Contributor

After double checking, I just realized that the exitTo bug is also happening on master, meaning I think we can ignore it for this PR. Thoughts?

@@ -1,4 +1,6 @@
import React, {Component} from 'react';
import {View} from 'react-native';
import get from 'lodash.get';
Copy link
Contributor

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

Copy link
Contributor Author

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)

Copy link
Contributor

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:

  1. There is no problem using underscore
  2. 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)
  3. Some underscore functionality we use doesn't exist in lodash (eg. findWhere() indexBy() pluck())
  4. Lodash methods can be imported individually for functionality that underscore doesn't have (but the vice versa is not true)
  5. Lodash is 24kb vs underscore 6kb and includes a lot of stuff that would never be used

Copy link
Contributor Author

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);
Copy link
Contributor

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

Copy link
Contributor

@tgolen tgolen left a 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)
Copy link
Contributor

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can't sign in to android Chat/hamburger flashes briefly before showing signin page

6 participants