useAuthenticator
@aws-amplify/ui-svelte ships with useAuthenticator Svelte composable that can be used to access, modify, and update Authenticator's auth state.
<script lang="ts">
import { Amplify } from 'aws-amplify';
import { useAuthenticator } from '@aws-amplify/ui-svelte'
import "@aws-amplify/ui-svelte/styles.css";
import awsconfig from './aws-exports';
import awsconfig from './amplify-output.json';
const { authenticator } = $derived(useAuthenticator());
</script>
Note that the authenticator is an object wrapped with $state, meaning we cannot destructure it:
<script>
import { useAuthenticator } from '@aws-amplify/ui-svelte';
const { authenticator: { signOut } } = $derived(useAuthenticator());
// ❌ This won't work because it breaks reactivity
</script>
In order to extract properties from the authenticator while keeping its reactivity, you need to use $derived.
<script setup>
import { useAuthenticator } from '@aws-amplify/ui-svelte';
// `route` `user` and `signOut` all are reactive.
const { authenticator } = $derived(useAuthenticator());
const { route, user, signOut } = $derived(authenticator);
</script>
Access Auth State
You can use useAuthenticator composable to access route string that represents the current authState. They can be one of:
idlesetupsignInsignUpconfirmSignInconfirmSignUpselectMfaTypesetupEmailsetupTotpforceNewPasswordforgotPasswordconfirmResetPasswordverifyUserconfirmVerifyUsersignOutauthenticated
<script lang="ts">
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-svelte';
const { authenticator } = $derived(useAuthenticator());
</script>
<Authenticator />
{#if authenticator.route === 'authenticated'}
<button onclick={authenticator.signOut}>Sign out</button>
{/if}
Authentication Check
If you just need to check if you're authenticated or not, you can use the more straightforward useAuthenticator composable to access the authStatus string. The authStatus string can represent the following states:
configuringauthenticatedunauthenticated
The
configuringstate only occurs when theAuthenticatoris first loading.
<script lang="ts">
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-svelte';
const { authenticator } = $derived(useAuthenticator());
</script>
<Authenticator />
{#if authenticator.authStatus === 'configuring'}
<button onclick={authenticator.signOut}>Loading...</button>
{/if}
{#if authenticator.authStatus === 'authenticated'}
<button onclick={authenticator.signOut}>Sign out</button>
{/if}
Access Authenticated User
You can use useAuthenticator composable to access current signed in user. If no user is authenticated, it'll return undefined.
<script>
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-svelte';
const {authenticator: a } = $derived(useAuthenticator());
</script>
<Authenticator />
{#if a.route === 'authenticated'}
<h1>Hello { a.user?.username }!</h1>
<button onclick={a.signOut}>Sign out</button>
{/if}
Trigger Transitions
You can use useAuthenticator composable to access functions that lets you trigger transitions to the authenticator. Please see Full API to see all supported transition functions. Any invalid transitions (e.g. signUp directly to authenticated) will be ignored.
<script>
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-svelte';
const {authenticator: a } = $derived(useAuthenticator());
</script>
<Authenticator />
{#if a.route === 'authenticated'}
<h1>Hello { a.user?.username }!</h1>
<button onclick={a.signOut}>Sign out</button>
{/if}
Example
Here's an example that uses the toForgotPassword trigger transition, to create a custom button. Note that example uses the Footer "slot" override.
<script lang="ts">
import { Authenticator, useAuthenticator } from '@aws-amplify/ui-svelte';
import '@aws-amplify/ui-svelte/styles.css';
const { authenticator: a } = $derived(useAuthenticator());
</script>
{#snippet signInFooter()}
<div style="text-align: center">
<button
onclick={a.toForgotPassword}
class="amplify-button amplify-field-group__control"
data-fullwidth="false"
data-size="small"
type="button"
style=" font-weight: normal"
>
Oh, no! Forgot Password?
</button>
</div>
{/snippet}
<Authenticator components={{
SignIn: {
Footer: signInFooter,
},
}}>
{#snippet children({ user, signOut })}
<h1>Hello {user.username}!</h1>
<button onclick={signOut}>Sign Out</button>
{/snippet}
</Authenticator>
Full API
These are readonly contexts that represent the current auth state. Any unapplicable context will be undefined.
| Name | Description | Type |
|---|---|---|
user | Current signed in user | AuthUser |
route | Name of the auth flow user is in | string |
error | Any error returned from service API call | string |
validationErrors | Any form validation errors found. Maps each error message to respective input name. | Record<string, string> |
hasValidationErrors | Whether there are any form validation errors | boolean |
isPending | Whether service API call is in progress | boolean |
codeDeliveryDetail | Provides detail on where confirm sign up code is sent to. | CodeDeliveryDetail |
allowedMfaTypes | Multi-factor authentication types available for selection. | AuthMfaType[] |
These helper functions trigger transition to another route. Note that any invalid transition (e.g. sign-in to authenticated directly) will be no-op.
| Name | Description | Type |
|---|---|---|
toSignIn | Transitions to signIn. Allowed from signUp, confirmSignUp, confirmSignIn, setupTotp, forgotPassword, and confirmResetPassword | () => void |
toSignUp | Transitions to signUp. Allowed from signIn. | () => void |
toForgotPassword | Transitions to forgotPassword. Allowed from signIn. | () => void |
skipVerification | Skips verification process. Allowed from verifyUser and confirmVerifyUser | () => void |