-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
c61f7b4
chore(docs): add all sections in headless auth usage page
sreeramsama ae0bd41
chore(docs): fix import
sreeramsama 5e6bd70
chore(docs): remove auth check section
sreeramsama deb2a37
Merge branch 'rna/docs-setup' into rna/docs-headless-usage
sreeramsama 9117964
chore(docs): fix imports, rn component usages and example code
sreeramsama File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
...ected-components/authenticator/headless/advanced/current-route.react-native.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
You can use `useAuthenticator` hook to access `route` string that represents the current `authState`. They can be one of: | ||
|
||
- `idle` | ||
- `setup` | ||
- `signIn` | ||
- `signUp` | ||
- `confirmSignIn` | ||
- `confirmSignUp` | ||
- `setupTOTP` | ||
- `forceNewPassword` | ||
- `resetPassword` | ||
- `confirmResetPassword` | ||
- `verifyUser` | ||
- `confirmVerifyUser` | ||
- `signOut` | ||
- `authenticated` | ||
|
||
```jsx | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
const App = () => { | ||
const { route } = useAuthenticator(context => [context.route]); | ||
|
||
// Use the value of route to decide which page to render | ||
return route === 'authenticated' ? <Home /> : <Authenticator />; | ||
}; | ||
``` |
17 changes: 17 additions & 0 deletions
17
...nected-components/authenticator/headless/advanced/current-user.react-native.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
You can use `useAuthenticator` hook to access current signed in `user`. If no user is authenticated, it'll return `undefined`. | ||
|
||
```jsx | ||
import { View, Text, Button } from 'react-native'; | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
const Home = () => { | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
|
||
return ( | ||
<View> | ||
<Text>{`Welcome, ${user.username}!`}</Text> | ||
<Button onPress={signOut} title="Sign Out" /> | ||
</View> | ||
); | ||
}; | ||
``` |
28 changes: 28 additions & 0 deletions
28
...]/connected-components/authenticator/headless/advanced/example.react-native.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
```jsx | ||
import { View, Button, Text } from 'react-native'; | ||
import { Amplify } from 'aws-amplify'; | ||
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
import awsExports from './aws-exports'; | ||
Amplify.configure(awsExports); | ||
|
||
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> | ||
); | ||
} | ||
``` |
1 change: 1 addition & 0 deletions
1
.../connected-components/authenticator/headless/advanced/full-api.react-native.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Below is the full list of context that `useAuthenticator` composable returns. |
12 changes: 12 additions & 0 deletions
12
...components/authenticator/headless/advanced/trigger-transitions.react-native.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
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. | ||
|
||
```jsx | ||
import { Button } from 'react-native'; | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
const Home = () => { | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
|
||
return <Button onPress={signOut} title={`Welcome, ${user.username}!`} />; | ||
}; | ||
``` |
60 changes: 60 additions & 0 deletions
60
...ed-components/authenticator/headless/advanced/useAuthenticator.react-native.mdx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { Tabs, TabItem } from '@aws-amplify/ui-react'; | ||
|
||
## useAuthenticator Hook | ||
|
||
`@aws-amplify/ui-react-native` ships with `useAuthenticator` React hook that can be used to access, modify, and update Authenticator's auth state. To use them, first wrap your application with [`<Authenticator.Provider>`](#authenticator-provider): | ||
|
||
```jsx | ||
import { Authenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
export default () => ( | ||
<Authenticator.Provider> | ||
<App /> | ||
</Authenticator.Provider> | ||
); | ||
``` | ||
|
||
Then, you can use `useAuthenticator` on your App: | ||
|
||
```jsx | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
const App = () => { | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
// ... | ||
}; | ||
``` | ||
|
||
## Authenticator Provider | ||
|
||
In advanced use cases where usage of the [`useAuthenticator` hook](headless#useauthenticator-hook) outside the scope of the [`Authenticator`](../authenticator) is needed, wrap your application inside an `Authenticator.Provider`. The `Authenticator.Provider` guarantees that the [useAuthenticator hook](headless#useauthenticator-hook) is available throughout your application. You can see an example of this pattern in the [Protected Routes Guide.](../../guides/auth-protected) | ||
|
||
```jsx | ||
import { View, Text } from 'react-native'; | ||
import { Authenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
export default function App() { | ||
return ( | ||
<Authenticator.Provider> | ||
<View> | ||
<Text>Your app here<Text/> | ||
</View> | ||
</Authenticator.Provider> | ||
); | ||
}; | ||
``` | ||
|
||
## Prevent Re-renders | ||
|
||
Using `useAuthenticator` hook at your `App` level is risky, because it'll trigger a re-render down its tree whenever any of its context changes value. | ||
|
||
To prevent undesired re-renders, you can pass a function to `useAuthenticator` that takes in Authenticator context and returns an array of desired context values. The hook will only trigger re-render if any of the array values change. | ||
|
||
For example, you can ensure `useAuthenticator` to only reevaluate when its `user` context changes: | ||
|
||
```jsx | ||
import { useAuthenticator } from '@aws-amplify/ui-react-native'; | ||
|
||
// hook below is only reevaluated when `user` changes | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.