-
Notifications
You must be signed in to change notification settings - Fork 297
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enable
useAuthenticator
usage outside <Authenticator /> (#1168)
* Initial draft * First attempt at useAuthenticator example * Allow children to be null * Remove initial state * Provider now only stores static ref to service * match new exports * add useAuthenticator test * remove unused variable * Create weak-stingrays-raise.md * Create aero-stingrays-raise.md * Fix typo * add more comments * Add comment on selector choice * useAuthenticator only returns selected values * Update changesets * update exports * Fix types * update exports list * Update .changeset/aero-stingrays-raise.md * Update .changeset/weak-stingrays-raise.md * selector returns an array of dependencies * Update test name * Remove unused variable * Update packages/ui/src/helpers/auth.ts * Update comments
- Loading branch information
Showing
16 changed files
with
339 additions
and
106 deletions.
There are no files selected for viewing
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,38 @@ | ||
--- | ||
"@aws-amplify/ui-react": minor | ||
--- | ||
|
||
This enables `useAuthenticator` usage outside <Authenticator /> to access commonly requested authenticator context like `user` and `route`. | ||
|
||
First wrap your App with `Authenticator.Provider`: | ||
|
||
```tsx | ||
const App = ( | ||
<Authenticator.Provider> | ||
<MyApp /> | ||
</Authenticator.Provider> | ||
) | ||
``` | ||
|
||
To avoid repeated re-renders, you can pass a function that takes in Authenticator context and returns an array of desired context values. This hook will only trigger re-render if any of the array value changes. | ||
|
||
```tsx | ||
const Home = () => { | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
|
||
return ( | ||
<> | ||
<h2>Welcome, {user.username}!</h2> | ||
<button onClick={signOut}>Sign Out</button> | ||
</> | ||
); | ||
}; | ||
|
||
const Login = () => <Authenticator />; | ||
|
||
function MyApp() { | ||
const { route } = useAuthenticator((context) => [context.route]); | ||
|
||
return route === 'authenticated' ? <Home /> : <Login />; | ||
} | ||
``` |
2 changes: 2 additions & 0 deletions
2
examples/next/pages/ui/components/authenticator/useAuthenticator/aws-exports.js
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,2 @@ | ||
import awsExports from '@environments/auth-with-username-no-attributes/src/aws-exports'; | ||
export default awsExports; |
34 changes: 34 additions & 0 deletions
34
examples/next/pages/ui/components/authenticator/useAuthenticator/index.page.tsx
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,34 @@ | ||
import { Amplify } from 'aws-amplify'; | ||
|
||
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-react'; | ||
import '@aws-amplify/ui-react/styles.css'; | ||
|
||
import awsExports from './aws-exports'; | ||
|
||
Amplify.configure(awsExports); | ||
|
||
const Home = () => { | ||
const { user, signOut } = useAuthenticator((context) => [context.user]); | ||
|
||
return ( | ||
<> | ||
<h2>Welcome, {user.username}!</h2> | ||
<button onClick={signOut}>Sign Out</button> | ||
</> | ||
); | ||
}; | ||
|
||
const Login = () => <Authenticator />; | ||
|
||
function App() { | ||
const { route } = useAuthenticator((context) => [context.route]); | ||
return route === 'authenticated' ? <Home /> : <Login />; | ||
} | ||
|
||
export default function AppWithProvider() { | ||
return ( | ||
<Authenticator.Provider> | ||
<App></App> | ||
</Authenticator.Provider> | ||
); | ||
} |
27 changes: 27 additions & 0 deletions
27
packages/e2e/features/ui/components/authenticator/useAuthenticator.feature
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 @@ | ||
Feature: Headless Usage | ||
|
||
Authenticator supports headless usage that provides access to current authenticator context | ||
outside the Authenticator component. React and Vue provides it through `useAuthenticator` | ||
hook and composable respectively, and Angular provides it through `AuthenticatorService` | ||
service. They can be used to access current authState (routes), authenticated user, etc. | ||
|
||
See https://ui.docs.amplify.aws/components/authenticator#headless for details. | ||
|
||
Background: | ||
Given I'm running the example "/ui/components/authenticator/useAuthenticator" | ||
|
||
@angular @react @vue @todo-angular @todo-vue | ||
Scenario: Conditionally render Login and Logout component | ||
|
||
/useAuthenticator example uses headless API to get access to conditionally render | ||
components for Login and Logout page. Both share the same authenticator context. | ||
|
||
When I type my "username" with status "CONFIRMED" | ||
And I type my password | ||
And I click the "Sign in" button | ||
Then I see "Sign out" | ||
When I reload the page | ||
Then I see "Sign out" | ||
And I click the "Sign out" button | ||
Then I see "Sign in" | ||
|
59 changes: 47 additions & 12 deletions
59
packages/react/src/components/Authenticator/Authenticator.tsx
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
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
67 changes: 0 additions & 67 deletions
67
packages/react/src/components/Authenticator/Provider/index.tsx
This file was deleted.
Oops, something went wrong.
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
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
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
Oops, something went wrong.