Authentication Methods
Scute provides a comprehensive set of authentication methods designed to handle various user scenarios while maintaining security and user experience. This guide explains the different authentication methods, their use cases, and how to implement them effectively.Authentication Flow Overview
Scute’s authentication system follows a hierarchical approach:- WebAuthn/Passkeys (highest security, best UX when available)
- Magic Links (secure, no password required)
- OTP (One-Time Password) (secure, works on all devices)
- OAuth/Social Login (convenient for users with existing accounts)
Core Authentication Methods
signInOrUp(identifier, options?)
The recommended unified authentication method that intelligently handles both sign-in and sign-up scenarios in a single call. This method provides the most seamless user experience by automatically selecting the best available authentication method.
Authentication Flow:
- WebAuthn Check: If supported and user has registered devices, attempts passkey authentication
- User Detection: Checks if the identifier exists in the system
- Smart Routing:
- Existing user → Sign-in flow
- New user → Sign-up flow
- Method Selection: Based on identifier type and app configuration:
- Email: Magic link (default) or OTP based on
email_auth_type - Phone: Always OTP
- Email: Magic link (default) or OTP based on
Use Cases:
- Universal “Continue with Email/Phone” buttons
- Simplified onboarding flows
- Applications wanting to minimize user friction
- Progressive web apps supporting multiple auth methods
Examples:
Response Handling:
signIn(identifier, options?)
For existing users only. This method will fail if the user doesn’t exist in the system, making it ideal for dedicated sign-in flows where you want to ensure only existing users can authenticate.
Key Features:
- Requires user to exist in the system
- Returns
IdentifierNotRecognizedErrorif user doesn’t exist - Still performs WebAuthn check first for registered users
- Respects app configuration for fallback methods
Use Cases:
- Dedicated “Sign In” forms separate from registration
- Applications with distinct sign-in/sign-up flows
- When you want to validate user existence before authentication
- Member-only areas or private applications
Examples:
WebAuthn Options:
signUp(identifier, options?)
For new users only. This method creates new user accounts and will fail if the user already exists, making it perfect for dedicated registration flows.
Key Features:
- Only creates new users
- Returns
IdentifierAlreadyExistsErrorif user exists - Accepts additional user metadata during registration
- No WebAuthn check (since it’s a new user)
- Sends verification based on identifier type and app config
Use Cases:
- Dedicated registration/sign-up forms
- Onboarding flows with user data collection
- Invitation-based registration
- Applications requiring explicit user consent for account creation
Examples:
User Metadata Schema:
Complete Registration Flow:
Direct Authentication Methods
sendLoginMagicLink(identifier, webauthnEnabled?)
Explicitly sends a magic link for authentication, bypassing any passkey checks. This method gives you direct control over the authentication flow when you specifically want email-based authentication.
Key Features:
- Bypasses WebAuthn/passkey authentication
- Works for both existing and new users
- Sends email with secure, time-limited authentication link
- Returns polling data to check verification status
Use Cases:
- “Continue with Email” buttons
- Users who prefer email-based authentication
- Fallback when WebAuthn fails or is unavailable
- Shared device scenarios where passkeys aren’t appropriate
Examples:
Magic Link Verification:
sendLoginOtp(identifier, webauthnEnabled?)
Explicitly sends a one-time password (OTP) for authentication. This method is ideal for phone-based authentication or when you need a more immediate verification method.
Key Features:
- Bypasses WebAuthn/passkey authentication
- Sends SMS or email-based OTP
- Works for both existing and new users
- 6-digit numeric code with time expiration
Use Cases:
- Phone number authentication
- Regions where SMS is preferred over email
- Two-factor authentication flows
- Users without reliable email access
- High-security applications
Examples:
OTP Verification:
signInWithOAuthProvider(provider)
Initiates OAuth authentication with external providers like Google, GitHub, or other configured OAuth services.
Key Features:
- Redirects to OAuth provider for authentication
- Handles federated identity management
- Supports multiple OAuth providers
- Automatic account linking for existing users
Use Cases:
- Social login (Google, Facebook, GitHub, etc.)
- Enterprise SSO integration
- Reducing password fatigue
- Accessing provider-specific APIs
- Streamlined onboarding for users with existing accounts
Examples:
OAuth Configuration Example:
Token-Based Authentication Methods
signInWithTokenPayload(payload)
Internal method used after successful verification of magic links, OTP, or OAuth to establish the user session. This method completes the authentication flow by setting up the user’s session.
Key Features:
- Establishes authenticated session
- Triggers authentication state change events
- Handles user data fetching
- Sets up session persistence
- Remembers user identifier for future logins
Use Cases:
- Completing magic link verification
- Completing OTP verification
- Handling OAuth callbacks
- Custom authentication flows
- Testing and development scenarios
Examples:
Token Payload Structure:
signOut()
Securely ends the user session and cleans up all authentication state. This method ensures complete logout across all stored data.
Key Features:
- Revokes refresh token on server
- Clears local session storage
- Removes stored credentials
- Triggers sign-out event for UI updates
- Works across browser tabs (broadcast channel)
Use Cases:
- User-initiated logout
- Session timeout handling
- Security-sensitive operations
- Switching between accounts
- Admin-initiated session termination
Examples:
Sign Out Return Value:
WebAuthn/Passkey Methods
addDevice()
Registers a new WebAuthn device/passkey for the currently authenticated user. This enhances security and provides passwordless authentication for future logins.
Key Features:
- Requires active authentication session
- Triggers browser’s WebAuthn UI
- Supports various authenticator types (platform, roaming)
- Stores credential for future authentication
- Emits registration events for UI feedback
Examples:
isWebauthnSupported()
Checks if WebAuthn is supported on the current device and browser.
isAnyDeviceRegistered()
Checks if the current user has any registered WebAuthn devices on this browser/device.
Authentication State Management
onAuthStateChange(callback)
Listen to authentication state changes to keep your UI synchronized with the user’s authentication status. This is the primary method for handling authentication events in your application.
Authentication Events:
signed_in- User successfully authenticatedsigned_out- User signed out or session endedinitial_session- Initial session check completedsession_refetch- Session data refreshedsession_expired- Session expired and needs renewaltoken_refreshed- Access token automatically refreshedmagic_pending- Magic link or OTP sent, waiting for verificationmagic_new_device_pending- Magic link sent due to new device detectionotp_pending- OTP sent, waiting for verificationotp_new_device_pending- OTP sent due to new device detectionwebauthn_register_start- WebAuthn registration startedwebauthn_register_success- WebAuthn registration completedwebauthn_verify_start- WebAuthn verification startedwebauthn_verify_success- WebAuthn verification completed
Examples:
Advanced State Management:
getUser(accessToken?)
Get the current authenticated user’s data or verify a specific access token.
Examples:
getAuthToken()
Get the current access token from the stored session.
Utility Methods
identifierExists(identifier)
Check if a user exists with the given email or phone number without initiating authentication.
getAppData(fresh?)
Get application configuration and settings to customize the authentication flow.
Session Management
listUserSessions()
Get all active sessions for the current user across all devices.
revokeSession(sessionId, credentialId?)
Revoke a specific session from another device.
removeDeviceCredential(credentialId)
Remove a WebAuthn credential without affecting the session.
Error Handling
Scute provides specific error types for different authentication scenarios. For comprehensive error handling documentation, see the Error Handling Guide.Best Practices
1. Start with signInOrUp for Maximum Flexibility
2. Implement Progressive Enhancement
3. Handle Network Connectivity
4. Implement Proper Loading States
5. Security Considerations
- Always use HTTPS in production to protect tokens and user data
- Validate user input before passing to authentication methods
- Implement session timeout handling using auth state events
- Use appropriate WebAuthn settings based on your security requirements