-
Notifications
You must be signed in to change notification settings - Fork 297
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
Enable useAuthenticator
usage outside <Authenticator />
#1168
Conversation
🦋 Changeset detectedLatest commit: 480d76f The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 1 alert when merging af70d1e into e0fcf36 - view on LGTM.com new alerts:
|
This pull request introduces 1 alert when merging 1e9b3d7 into e0fcf36 - view on LGTM.com new alerts:
|
This pull request introduces 1 alert when merging c361bb9 into e0fcf36 - 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!
packages/react/src/components/Authenticator/hooks/useAuthenticator/index.tsx
Outdated
Show resolved
Hide resolved
packages/react/src/components/Authenticator/hooks/useAuthenticator/index.tsx
Outdated
Show resolved
Hide resolved
* Initial draft for React * Remove full-headless related content * Restore custom slot example * First attempt at useAuthenticator example * Initial draft * First attempt at useAuthenticator example * Allow children to be null * Remove initial state * Provider now only stores static ref to service * match new exports * add useAuthenticator test * remove unused variable * Create weak-stingrays-raise.md * Create aero-stingrays-raise.md * Fix typo * add more comments * Add comment on selector choice * useAuthenticator only returns selected values * Update changesets * update exports * Fix types * update exports list * Update .changeset/aero-stingrays-raise.md * Update .changeset/weak-stingrays-raise.md * Update react documentation to match #1168 * revert non pr related chagnes * Add angular example * Trigger tests * run angular tests * Added Vue examples and documentation * Vue tests will run * selector returns an array of dependencies * Update test name * Update react documentation to match latest #1168 * Add angular docs * Correct full-api link * Remove unused variable * Add instructions for injecting AuthenticatorService * Remove unused export * Remove lingering comment * Change 'conditional-rendering' to `current-route' * Send INIT event from I18n demo * Fix typos * wordiness Co-authored-by: Erik Hanchett <[email protected]>
Issue #, if available: #637, #254, #664, #640.
Description of changes: This PR enables
useAuthenticator
hook to be used outsideAuthenticator
. Also adds/useAuthenticator
example and e2e tests that'll supplement docs PR #1075.Use Case
Main use case is custom routing (#1058), which is not possible today because Authenticator assumes SPA usage and requires Authenticated content to be nested inside
Authenticator
. #1130 puts it well:Problem
Internally, we used
useAuthenticator
andAuthenticator.Provider
to shareauth
contexts. To useuseAuthenticator
outsideAuthenticator
, we falsely went around this issue by wrapping<Authenticator.Provider />
around their app:Problem here was that two providers create two different xstate machines.
useAuthenticator
from<App />
(2) returned false data because it is referencing the wrong provider (1) instead of (4).Wasted re-renders
Another React nuance we overlooked is that
useAuthenticator
changes on every xstate transitions, including form input events!This is not only computationally expensive but also can trigger repeated mounting of
<Authenticator />
becauseMyApp
(1) encompasses it. This opens up crucial bugs like form losing its values on re-render. xstate docs explains better than I can:Solutions
Sync Provider Context Value
Now
Authenticator.Provider
detects whether there's anotherProvider
above it. If so, it'll just pass that parent value. This resolves multiple xstate machine instantiations.Serve static value from
Authenticator.Provider
Now
Authenticator.Provider
only maintains a static reference toauthMachine
service to prevent re-renders, as suggested from xstate docs.useAuthenticator
instead listens to state changes and provides respective auth context.Split
Slot
logic to internal hookWe've been using
useAuthenticator
to share custom components for Authenticator. This is only needed internally, is static, and is not needed externally. This has been split touseCustomComponents
hook to focusAuthenticator.Provider
on justauthMachine
contexts. Splitting up complex context value is encouraged to reduce wasted re-renders: facebook/react#15156.Add optimization mechanism to useAuthenticator
Even with static served
Authenticator.Provider
,useAuthenticator
still changes frequently on every state change. This PR addsuseAuthenticatorRoute
anduseAuthenticatorUser
that only triggers re-render wheneverroute
oruser
change respectively. This provides developers control over when they want auth updates.Under the hood, this is done by
selector
optional parameter to baseuseAuthenticator
.selector
is a function that accepts current facade values and returns desired value(s) that should trigger re-render.This uses
xstate
'suseSelector
, which provides just what we need:By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.