-
Notifications
You must be signed in to change notification settings - Fork 317
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
chore(docs): add headless auth usage page #3045
Conversation
|
...ges/[platform]/connected-components/authenticator/headless/advanced/example.react-native.mdx
Outdated
Show resolved
Hide resolved
### Authentication Check | ||
|
||
If you just need to check if you're authenticated or not, you can use the more straightforward `useAuthenticator` hook to access the `authStatus` string. The `authStatus` string can represent the following states: | ||
|
||
- `configuring` | ||
- `authenticated` | ||
- `unauthenticated` | ||
|
||
> The `configuring` state only occurs when the `Authenticator` is first loading. | ||
|
||
```tsx{1,4-7} | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
const App = () => { | ||
const { authStatus } = useAuthenticator(context => [context.authStatus]); | ||
|
||
// Use the value of authStatus to decide which page to render | ||
return ( | ||
<> | ||
{authStatus === 'configuring' && 'Loading...'} | ||
{authStatus !== 'authenticated' ? <Authenticator /> : <Home />} | ||
</> | ||
); | ||
}; | ||
``` |
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.
Let's remove this. I'm not entirely sure that authStatus
works as expected
const Home = () => { | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
|
||
return <button onClick={signOut}>Welcome, {user.username}!</button>; |
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.
return <button onClick={signOut}>Welcome, {user.username}!</button>; | |
return <Button onPress={signOut} title={`Welcome, ${user.username}!`} />; |
You can use `useAuthenticator` hook to access functions that lets you trigger transitions to the authenticator. Please see [Full API](#full-api) to see all supported transition functions. Any invalid transitions (e.g. `signUp` directly to `authenticated`) will be ignored. | ||
|
||
```tsx{1,4-6} | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; |
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.
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | |
import { Button } from 'react-native'; | |
import { useAuthenticator } from '@aws-amplify/ui-react-native'; |
import { Authenticator } from '@aws-amplify/ui-react-native'; | ||
import { View } from 'react-native'; |
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.
Not sure if this how all our docs are but generally think it's beet to place React and React Native imports above ours:
import { Authenticator } from '@aws-amplify/ui-react-native'; | |
import { View } from 'react-native'; | |
import { Text, View } from 'react-native'; | |
import { Authenticator } from '@aws-amplify/ui-react-native'; |
export default function App() { | ||
return ( | ||
<Authenticator.Provider> | ||
<View>Your app here</View> |
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.
React Native can only render strings within a Text component:
<View>Your app here</View> | |
<View> | |
<Text>Your app here</Text> | |
</View> |
@@ -0,0 +1,16 @@ | |||
You can use `useAuthenticator` hook to access current signed in `user`. If no user is authenticated, it'll return `undefined`. | |||
|
|||
```tsx{1,4,8-9} |
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.
Are we sure we need the line highlights?
export default function App() { | ||
const components = { | ||
SignIn: { | ||
Footer() { | ||
const { toResetPassword } = useAuthenticator(); | ||
return ( | ||
<View> | ||
<Button onPress={toResetPassword}> | ||
Custom Reset Password | ||
</Button> | ||
</View> | ||
); | ||
}, | ||
}, | ||
}; | ||
|
||
return ( | ||
<Authenticator components={components}> | ||
{({ signOut, user }) => ( | ||
<View> | ||
<Text>Hello {user.username}</Text> | ||
<Button onPress={signOut}>Sign out</Button> | ||
</View> | ||
)} | ||
</Authenticator> | ||
); | ||
} |
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.
The components prop does not behave the same between React and React Native:
export default function App() { | |
const components = { | |
SignIn: { | |
Footer() { | |
const { toResetPassword } = useAuthenticator(); | |
return ( | |
<View> | |
<Button onPress={toResetPassword}> | |
Custom Reset Password | |
</Button> | |
</View> | |
); | |
}, | |
}, | |
}; | |
return ( | |
<Authenticator components={components}> | |
{({ signOut, user }) => ( | |
<View> | |
<Text>Hello {user.username}</Text> | |
<Button onPress={signOut}>Sign out</Button> | |
</View> | |
)} | |
</Authenticator> | |
); | |
} | |
function Footer() { | |
const { toResetPassword } = useAuthenticator(); | |
return ( | |
<Button onPress={toResetPassword} title="Custom Reset Password" /> | |
); | |
}, | |
export default function App() { | |
return ( | |
<Authenticator components={{ | |
SignIn: (props) => <Authenticator.SignIn {...props} Footer={Footer} /> | |
}}> | |
<View> | |
<Text>Hello {user.username}</Text> | |
<Button onPress={signOut} title="Sign out" /> | |
</View> | |
</Authenticator> | |
); | |
} |
|
||
Then, you can use `useAuthenticator` on your App: | ||
|
||
```tsx |
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.
Can we align all the examples as being jsx
Description of changes
Add headless auth usage page
Issue #, if available
Description of how you validated changes
Checklist
yarn test
passessideEffects
field updatedBy submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.