Closed jbowen28 closed 1 year ago
@jbowen28 From your code above, it looks like you are not using the Authenticator
UI component in your code. The Authenticator.Provider
will not work correctly without Authenticator
component being rendered in the DOM. We are working on providing React hooks which will allow you to keep track of auth state without the Authenticator UI being rendered, but that's not currently possible.
If you don't want to use the Authenticator UI, you probably don't need the Authenticator.Provider
hook, and should use the Amplify Auth JS API's directly. Please lLet me know if I'm understanding your use case correctly!
I was following the instructions at https://ui.docs.amplify.aws/react/connected-components/authenticator/headless. No where in those instructions does it indicate that the Authenticator UI component ( I am not exactly sure which component that is) needs to be used. From the title of that web page "Headless - For advanced use cases you can run the Authenticator in a headless mode", I assumed that I did not have to use any AWS Authenticator UI (display) components. I do not want to use the AWS Authentication UI components because they use a different UI component framework than the rest of my app, which uses MUI. Is there a way to use Authenticator.Provider context and useAuthenticator hook as I hoping to (and as indicated by the AWS instructions) by instantiating a Authenticator UI component without rendering it?
@jbowen28, I agree, I think this is a miss in the documentation. I'll get that updated.
Yes, if you still want to use the Authenticator.Provider
, you can do so by rendering a visually hidden Authenticator like:
CSS:
.hide-authenticator {
display: none;
}
JSX:
<Authenticator className="hide-authenticator">
Just note that the Authenticator.Provider
keeps track primarily of the state of the Authenticator UI, so route
refers to the screen being displayed in the UI, and user
may not be updated if you make changes such as updating the user attributes (in that case using Auth.currentAuthenticatedUser
may work better).
EDIT: Updated to show how to pass a classname in since I realized that Authenticator
doesn't take the style prop.
useAuthenticator
hook documentation updated in latest docs.
Regarding using the hook without the Authenticator
component, that is being tracked separately https://github.com/aws-amplify/amplify-ui/issues/2634
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?
CRA
What browsers are you seeing the problem on?
Chrome
Please describe your bug.
I try to get the auth context from Authenticator.Provider using useAuthenticator but user, error, and isPending are always undefined. Only the route context is undefined but does not appear to show all auth states. Also, the component which uses useAuthenticator does not rerender when auth context changes.
What's the expected behaviour?
The auth context from Authenticator.Provider using useAuthenticator is not undefined and updated when the auth context changes. The component which uses useAuthenticator rerenders when auth context changes.
Help us reproduce the bug!
Code Snippet
Additional information and screenshots
On app initial load, the browser console showed the following:
After logging-in the browser console log showed the following:
* edited by wlee221: code formatting