-
Notifications
You must be signed in to change notification settings - Fork 314
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
fix(authenticator): fetch current user on routed apps after refresh #1580
Conversation
🦋 Changeset detectedLatest commit: 111bf94 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
This pull request introduces 2 alerts when merging fc71b28 into eedae23 - view on LGTM.com new alerts:
|
@@ -25,47 +25,58 @@ export function createAuthenticatorMachine() { | |||
context: { | |||
user: undefined, | |||
config: {}, | |||
services: {}, | |||
services: { ...defaultServices }, | |||
actorRef: undefined, | |||
}, | |||
states: { | |||
// See: https://xstate.js.org/docs/guides/communication.html#invoking-promises | |||
idle: { |
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.
idle
state now starts with Auth.currentAuthenticatedUser
call.
Knowing that, idle
is a bad name (it's on me!) and should be named more explicitly. I scoped this out of this PR, because that would change our route
names and I would rather deal with it in 3.0 changes.
target: 'authenticated', | ||
initial: 'waitConfig', | ||
states: { | ||
waitConfig: { |
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.
At this stage, we confirmed that no user has signed in yet. Now we wait for Authenticator
to send INIT
event to pass machine props like initialState
.
}, | ||
onError: [ | ||
}, | ||
applyConfig: { |
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.
Once we have all the machine props ready, we apply those configs to get loginMechanisms
, etc.
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.
👍
// send INIT event once machine is at 'setup' state | ||
this.authenticator.subscribe(() => { | ||
const { route } = this.authenticator; | ||
if (!this.hasInitialized && route === 'setup') { |
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.
Previously authMachine
got stuck in idle
state until an Authenticator
in DOM sends an INIT
event on mount. Now authMachine
starts independently (see state machine changes below), and will explicitly ask for config from Authenticator
once it's in setup
state.
Once Authenticator
sees that state is in setup
, it'll send the usual INIT
event.
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.
Granted I am still learning how xstate
and the Authenticator
state machine work but LGTM 👍🏽. Left some questions
Co-authored-by: Caleb Pollman <cpollman@amazon.com>
…plify/amplify-ui into get-user-on-refresh/state-machine
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.
LGTM 🚢 Thanks for addressing the feedback!
* Angular Example * React example * Vue examples * Add missing ! * Update useAuthenticator test * Use slots on parent route * Configure Amplify on `/home` * Add react sign out button * Update wording on react example * Add sign out test * Add angular sign out buttons * Sign Out button in Vue * destructure signOut ref * Add hidden Authenticator on Vue * Fix copy paste typo
This pull request introduces 3 alerts when merging 43119e4 into 8278c72 - view on LGTM.com new alerts:
|
…plify/amplify-ui into get-user-on-refresh/state-machine
fyi, I added 9 liner commit b8c36bb to fix a autoSignIn test failure. I'll merge this tomorrow morning unless there are objections! |
This pull request introduces 3 alerts when merging 111bf94 into 683eac9 - view on LGTM.com new alerts:
|
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.
Looks good to me!
Problem
Previously,
useAuthenticator
hook/composable/service required an instance of Authenticator in the DOM tree to initiate the machine and look for any current authenticated user in the local storage.This caused problems when a user refreshed in an authenticated route. For example, imagine the following structure:
/
is the entry to the home page and has the<Authenticator />
instance to enable sign in./my-account
is an authenticated route that users can go after they signed in. This doesn't have<Authenticator />
in DOM because they already signed in.Whenever a user refreshed on
/my-account
, the page wouldn't have anAuthenticator
in the DOM to initiate the authMachine, and so auth contexts from the hooks returned undefined.Fix
This PR adjusts
useAuthenticator
hooks so that it first looks forAuth.currentAuthenticatedUser
. If there are, it'll just transition directly toauthenticated
route and does not require an instance of<Authenticator />
whatsoever. Otherwise, it would proceed to thesetup
state, in which it will wait forAuthenticator
to send theINIT
event.From the framework side,
<Authenticator />
will subscribe to authMachine changes. Once the machine gets tosetup
state and is ready for theINIT
event, authenticator will send an INIT event with all the machine options.Remaining Problems
Note that there are still bugs we have to address for routed app use cases, namely #1581. This PR will at least correct the refresh behavior and remove the need for
Authenticator
in the DOM tree for machines to start.Issue #, if available
Related to #1332, #1497
Description of how you validated changes
#1578 adds new examples and tests for refreshing. #1578 is to be merged into this PR once approved.
Checklist
yarn test
passesBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.