Skip to content
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

How to signOut in a child component #1198

Closed
utopianf opened this issue Jan 27, 2022 · 2 comments
Closed

How to signOut in a child component #1198

utopianf opened this issue Jan 27, 2022 · 2 comments
Labels
Authenticator An issue or a feature-request for an Authenticator UI Component React An issue or a feature-request for React platform

Comments

@utopianf
Copy link

I found the example code in the document and it works great.

<Authenticator>
  {({ signOut, user }) => (
    <main>
      <h1>Hello {user.username}</h1>
      <button onClick={signOut}>Sign out</button>
    </main>
  )}
</Authenticator>

Next, I want to use signOut in a layout component, so I tried the code below, but nothing happened and the console.log(user) shows my account detail.

import { useAuthenticator } from "@aws-amplify/ui-react"

export const Navbar = () => {

  const { signOut, user } = useAuthenticator()
  console.log(user)

  return (
    <>
      ...
        <MenuItem>
          <ListItemIcon onClick={signOut}>
            <Logout fontSize="small" />
          </ListItemIcon>
          Logout
        </MenuItem>
      ...
    </>
  )
}

I would appreciate it a lot if someone can give a solution.

@wlee221 wlee221 added React An issue or a feature-request for React platform Authenticator An issue or a feature-request for an Authenticator UI Component labels Jan 28, 2022
@wlee221
Copy link
Contributor

wlee221 commented Jan 28, 2022

Hi, this use case should now be possible with #1168, which allows useAuthenticator to be used outside Authenticator component.

Please wrap your App (or whichever parent that contains both your Authenticator and Layout component with Authenticator.Provider, then you should be able to use useAuthenticator to sign out users and trigger transitions.

Please see https://ui.docs.amplify.aws/components/authenticator#useauthenticator-hook for full instructions. Particularly, you need to pass a parameter to useAuthenticator as described in the docs.

@wlee221
Copy link
Contributor

wlee221 commented Jan 28, 2022

This is a fairly new use case we support, so we'd gladly appreciate any feedbacks!

@wlee221 wlee221 closed this as completed Jan 28, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Authenticator An issue or a feature-request for an Authenticator UI Component React An issue or a feature-request for React platform
Projects
None yet
Development

No branches or pull requests

2 participants