Overview
TheLinkAuth component provides a comprehensive solution for managing linked authentication methods for the current user. It allows users to link or unlink multiple authentication methods (email, phone, OAuth providers) to their account, providing a flexible and composable interface for building custom authentication management experiences.
The core features include:
- Support for multiple authentication methods (email, phone, OAuth providers)
- Multi-step authentication linking process
- Composable UI that gives developers full control over layout and styling
- Smooth transitions between authentication method views
- Centralized state management for the link auth process
Architecture
The component is built using a composition pattern that enables maximum flexibility while providing sensible defaults when customization is not needed.Composition model
TheLinkAuth component is composed of several subcomponents that work together to create the complete authentication management experience. This approach allows developers to customize the UI structure while maintaining the underlying functionality.
The main components are:
LinkAuth: The root wrapper component that provides theLinkAuthContextand manages stateLinkAuthTitle: Renders the title for the current viewLinkAuthItems: Renders the list of available authentication methodsLinkAuthItem: Renders an individual authentication method itemLinkAuthFlow: Manages the multi-view flow for linking authentication methodsLinkAuthFlowBackButton: A button to navigate back to the list viewLinkAuthError: Displays error messages when linking fails
LinkAuth
TheLinkAuth component accepts a children prop that can be either React nodes or a render function. When using a render function, it receives the current LinkAuthState as an argument, providing access to all state values without needing to use the useLinkAuthContext hook directly.
Render function signature:
LinkAuthState object contains:
methodToLink: The authentication method currently being linked (ornull)authMethods: Array of available authentication methods with link statuserror: Error message if linking failed (ornull)isPending: Boolean indicating if a linking operation is in progress
children as a render function:
useLinkAuthContext.
State management (LinkAuthProvider and LinkAuthContext)
The entire link auth flow’s state is managed byLinkAuthProvider and accessed via the useLinkAuthContext hook. This context contains:
methodToLink: The authentication method currently being linked (if any)error: Any error message that occurred during the linking processdispatch: Function to update the link auth state
LINK_AUTH_METHOD: Set the authentication method to linkLINK_AUTH_METHOD_ERROR: Set an error messageRESET_STATE: Reset the state back to the initial list view
LinkAuthFlowProvider and useLinkAuthFlow
TheLinkAuthFlowProvider manages the flow-specific context and provides helper functions for navigation and actions. The useLinkAuthFlow hook provides access to:
link: Function to initiate linking an authentication methodback: Function to navigate back in the flowonLinkSuccess: Callback when linking is successful
LinkAuthFlow
TheLinkAuthFlow component manages the display of different views based on the state. It handles transitions between:
list: The main view showing all available authentication methodsemail: The email linking flowsms: The phone number linking flow
LinkAuthFlow provides a children render prop that allows you to wrap or augment the content for each view. This is particularly useful for adding view-specific styling or additional UI elements.
Render function signature:
view: The current view being rendered ("list","email", or"sms")Content: The rendered content for that view
LinkAuthItems
TheLinkAuthItems component renders the list of available authentication methods. By default, it uses the authMethods from the app config and automatically renders LinkAuthItem components for each method. It accepts an onLink callback to customize what happens when a user selects a method to link.
Render function (optional):
LinkAuthItems provides a children render prop that allows you to customize how individual authentication method items are rendered. This is useful if you want to use your own custom UI for the list items while still leveraging the LinkAuthItems logic for managing the list.
Render function signature:
authMethod: The authentication method ("email","sms","oauth:google", etc.)icon: The icon element for the methodlabel: The display label for the methodisLinked: Boolean indicating if the method is already linkeduserAlias: The linked identifier (email address, phone number, etc.)isPending: Boolean indicating if this method is currently being linkedonLink: Function to call when linking this method
LinkAuthItems will render the default LinkAuthItem component for each method.
LinkAuthItem
TheLinkAuthItem component renders an individual authentication method with:
- An icon representing the authentication method
- The method name
- The user alias (e.g. email address, phone number, username) if already linked
- A link button (if not already linked)
- A disabled unlink button (if already linked)
Example: Basic usage
Render aLinkAuth component with an onLinkSuccess handler:
Example: Accessing state
Display a message based on the LinkAuth state:Advanced example: Using SignInModal
Customize LinkAuthItems to show a modal for non-OAuth methods instead of transitioning in place:Notes
- Unlinking authentication methods is not yet implemented. Users can add multiple authentication methods to their account, but cannot currently remove them.
- OAuth providers perform a full page redirect for authentication. The user will temporarily leave your application to authenticate with the provider and then be redirected back.