Overview
TheSignInPage component is a page-level component that automatically initiates the Logto authentication flow when rendered. It handles both standard redirects and popup-based authentication.
Props
This component does not accept any props. All configuration is managed through theAuthProvider.
Usage
Basic Setup
Create a sign-in route in your application (e.g.,/signin or /login):
Next.js App Router
Next.js Pages Router
React Router
Behavior
Standard Flow
- Component checks if user is already authenticated
- If not authenticated, initiates Logto sign-in flow
- User is redirected to Logto’s authentication page
- After authentication, user is redirected to the callback URL
Popup Flow
When popup sign-in is enabled (enablePopupSignIn={true} in AuthProvider):
- Component detects it’s opened in a popup window (via URL param
?popup=true) - Sets
simple_logto_popup_flowflag in sessionStorage - Initiates sign-in with popup disabled to prevent nested popups
- After authentication, notifies parent window and closes popup
Already Authenticated
If user is already signed in: Standard window:- Redirects to home page (
/) if not already there - Reloads the page if already on home page
- Sends
SIGNIN_COMPLETEmessage to parent window - Falls back to localStorage if
postMessagefails - Closes the popup after 100ms delay
Loading State
While checking authentication state:- Displays a centered loading spinner
- Prevents any sign-in actions until state is determined
Features
- Auto-Redirect: Automatically initiates sign-in without user interaction
- Popup Detection: Intelligently detects and handles popup authentication
- State Persistence: Uses sessionStorage to maintain popup state across redirects
- Fallback Messaging: Multiple communication methods between popup and parent window
- Prevents Nested Popups: Disables popup mode when already in a popup
- Loading Feedback: Built-in loading state while determining auth status
Implementation Details
Popup Detection
The component detects popup mode through:- URL Parameter:
?popup=truequery string - SessionStorage:
simple_logto_popup_flowflag (persists across redirects) - Window Opener: Presence of
window.openerreference
Parent Window Communication
When in popup mode, the component communicates with the parent window via:- Primary:
window.opener.postMessage()with same-origin check - Fallback:
localStorage.setItem('simple_logto_signin_complete')for broadcast
Sign-In Prevention
The component uses a ref (signInInProgress) to prevent multiple simultaneous sign-in attempts.
Notes
- The component must be used within an
AuthProvidercontext - No visual UI is rendered after the loading state (redirects immediately)
- The
/signinroute is expected by the default popup implementation inAuthProvider - If you use a different route, update popup calls accordingly
- The component automatically handles cleanup of sessionStorage flags
Related
- CallbackPage - Handles the callback after authentication
- AuthProvider - Configure authentication behavior
- useAuth - Access authentication state programmatically