-
Notifications
You must be signed in to change notification settings - Fork 313
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
Discussion: Use cases for useAuthenticator
Hook
#1497
Comments
Is there a recommended workaround? |
Yep, this is a bug -- I believe the root cause is the same as #1332. as of now needs to be in the component tree for it to properly transition to the route 'authenticated' . Meaning whenever you're on As per workaround, can you try putting an "invisible" authenticator inside your |
I'm not sure I understand. Is adding
Also I'm not sure what you mean by "invisible" authenticator. I have the actual |
Yep your comment on React The root cause is that
which is an anti-pattern in React and what we'll prioritize fixing. That's why I mentioned an invisible In terms of your first problem, that is app-level re-rendering, do you have the same problem if you have |
I guess I mean to say: how does one incorporate an invisible |
@vymao Could you try adding an
|
Seems to work that way, thanks. But I think this still isn't ideal; my app has a separate login page that actually uses |
Yep, +1 on it not being ideal at all, and It's an anti-pattern. We'll have a holistic fix soon. |
Is this issue underlying the behavior I'm seeing, described in this SO? |
@davegravy yep, correct. Taking a look at this now. |
Wanted to come back to this and mention that @wlee221 and @ErikCH the solution involving hiding Authenticator seems to only work if the user is signed in. If the user is signed out, the Authenticator still appears. To provide more context for how I used this:
Such a layout is like:
which imports the CSS file containing:
|
Hi @vymao ! Does this problem still occur? |
Yes. Was there an update that fixed this? |
Hi @vymao ! Yes, so we made a change with #1580 that improves the experiences for users that are on multiple routes. So now as long as you have your application surrounded by There is still one more outstanding issue with this solution. On refresh the I created a guide on authenticated routes here. In it I describe this scenario, and work around for it. In this scenario if someone goes to an authenticated route, and it's an Let me know if that's what you're experiencing and if this work around helps in the mean time. |
It seems to work for now, will report back if any issues. Thanks! |
@adriaanbalt That was a mistake on my end. It's available as a prop passed in to the
|
@calebpollman Question: Why is this information so difficult to find and not an obvious alternative? |
@adriaanbalt Glad that you got things working!
The documentation could be improved here, think the addition of a concrete example for this use case would be a good starting point but curious if there is anything else that you would have found helpful? |
Generally, the documentation is good, but since time has changed the capabilities it seems like it is a bit dated. I'm using React Native but this approach also works with React or JavaScript. Firstly, the documentation relies on examples. There isn't the classic documentation style that outlines each property, class or method that is available on each hook or module. This would already be helpful and probably easier to maintain. When something is deprecated the page that the information is displayed can also say it has been deprecated. This is relatively common when reviewing documentation for other APIs. Secondly, currently the AWS Amplify customization docs suggest this approach but this is not quite right because the NBM "module itself seems to handle the revealing of the children (see my earlier post's "Other finds" section). Lastly, if you search for "handlesubmit" on the authenticator documentation page the word doesn't exist For an example, you can go with the a basic approach, which satisfies almost all use-cases. Use case: I want to add custom authentication to my app. Provider
MySignIn component
Note the use of You can see the documentation suggests using I would suggest including information at this point in the documentation that explains the use of I hope this helps and wish you luck improving the docs. Thanks! |
I am a little confused by this thread. I tried to implement this with Javascript and <Authenticator components={{
SignIn: props => {
return <MyAuthentication {...props}/>
},
SignUp: props => {
return <MyAuthentication {...props}/>
}
}}>
<BrowserRouter>
<App />
</BrowserRouter>
</Authenticator> |
Hi @annjawn, |
Ah ok, I read the previous comment which indicated that it may work. Thanks. so the only option for a fully custom Auth screen experience is to fall back to |
so
🤦 |
There is a new version of the Authenticator in the works which will allow full UI customization without having to recreate all the auth flow logic, but it's not available yet. Yes, you can definitely create your own auth experience using the Amplify Auth JS apis here: |
I had much better luck with the Authenticator component and provider when I was using I only use I found a workaround for that issue. I'm also using redux, and at signOut, I do not use the
And my sign-out button handler then does this:
Then, this was the trick that got things working for me: forcing a rerender of the Provider upon logout via redux:
With this workaround, I am able to use amplify v6 with the Authenticator component and logout successfully. Hope it helps! Good luck. |
@annjawn implementation is using React Native in a web build configuration as I'm building a project that must work across all platforms. The other comments should also help you though :) |
Hi @adriaanbalt, so fully overriding the components in |
Ok, I can confirm that using only React+Vite App.jsx const Login = React.lazy(() => import('./pages/Authentication/Login'))
const SignUp = React.lazy(() => import('./pages/Authentication/Signup'))
const Reset = React.lazy(() => import('./pages/Authentication/Reset'))
function App() {
const { authStatus } = useAuthenticator((context) => [context.user]);
return (
<>
{
(authStatus === "authenticated")?
<Routes>
<Route path="/" element={<AppLayout />}>
.....//all the app authenticated routes
</Route>
</Routes>
:<Routes>
<Route path="/" element={<Authenticate />}>
<Route index element={<React.Suspense fallback={<Spin/>}>
<Login/> //--> fully custom UI using aws-amplify/auth
</React.Suspense>} />
<Route path="signup" element={<React.Suspense fallback={<Spin/>}>
<SignUp/> //--> fully custom UI using aws-amplify/auth
</React.Suspense>} />
<Route path="reset" element={<React.Suspense fallback={<Spin/>}>
<Reset/> //--> fully custom UI using aws-amplify/auth
</React.Suspense>} />
</Route>
</Routes>
}
</>
)
} main.jsx ReactDOM.createRoot(document.getElementById('root')).render(
<React.StrictMode>
<Authenticator.Provider>
<BrowserRouter>
<App />
</BrowserRouter>
</Authenticator.Provider>
</React.StrictMode>,
) AppLayout.jsx (has SignOut button) const { signOut } = useAuthenticator((context) => [context.user]);
const onSignOut = () => {
signOut()
}
return (<button onClick={onSignOut}>Log out</button>) Actually quite shocked to see how well this works. |
useAuthenticator
Hook
My
on
I get undefined and then the user is populated |
@williamtorc are you using the Authenticator component with the user of useAuthenticator? Similar to this comment here |
Closing this issue as the original bug has been resolved. If you have any new issues please open a new ticket. |
The original issue has since been resolved, however, leaving this open due to good discussion around use cases for the
useAuthenticator
hookOriginal ticket below:
Before creating a new issue, please confirm:
On which framework/platform are you having an issue?
React
Which UI component?
Authenticator
How is your app built?
Create React App
Please describe your bug.
I am trying to use the
useAuthenticator
hook to set a global authentication state.However, several problems have arisen:
const { user, signOut } = useAuthenticator((context) => [context.user]);
, as is mentioned here, there is no app-level re-rendering. So several other components that are also using the hook and are dependent on authentication state don't change.What's the expected behaviour?
When I sign in or out, React should trigger an app-level re-render with the new authentication state. Furthermore, if I refresh the page, the authentication state should not change.
Help us reproduce the bug!
Following the instructions, I added
Authenticator.Provider
at the app level:I use
useAuthenticator
to Login:I define
const { user, signOut } = useAuthenticator((context) => [context.user]);
in components, not at the app level, where I want to obtain the authentication state. Following the guide, I use the conditionaltypeof user === 'undefined'
to see if the user is authenticated.Code Snippet
// Put your code below this line.
Additional information and screenshots
No response
The text was updated successfully, but these errors were encountered: