Installation
@niledatabase/client provides client-side functions for managing authentication, sessions, and user accounts. It is designed to work seamlessly with @niledatabase/server which provides the server-side routes.
Usage
The library exports a singleton instance of theAuthorizer class, named auth, which is pre-configured for most use cases.
Authentication
signIn
Signs in a user. Supports both credentials (email/password) and Single Sign-On (SSO) providers.Parameters
| Name | Type | Description |
|---|---|---|
provider | string | The authentication provider (e.g., 'credentials', 'google', 'github'). |
options | object | Configuration options. |
| Name | Type | Description |
|---|---|---|
email | string | User’s email (required for 'credentials'). |
password | string | User’s password (required for 'credentials'). |
callbackUrl | string | URL to redirect to after successful sign-in. |
redirect | boolean | If false, prevents automatic redirection (useful for handling responses manually). |
signUp
Creates a new user account. Can optionally create a new tenant for the user.Parameters
| Name | Type | Description |
|---|---|---|
options | object | Sign-up configuration. |
| Name | Type | Description |
|---|---|---|
email | string | User’s email address. |
password | string | User’s password. |
createTenant | boolean | string | If true, creates a tenant named after the user’s email. If a string, creates a tenant with that specific name. |
callbackUrl | string | URL to redirect to after successful sign-up. |
signOut
Logs the current user out and clears the session.Parameters
| Name | Type | Description |
|---|---|---|
options | object | Sign-out configuration. |
| Name | Type | Description |
|---|---|---|
callbackUrl | string | URL to redirect to after sign-out. |
Session Management
getSession
Retrieves the current user session. If a valid session is cached, it is returned; otherwise, a request is made to the server.Password Management
resetPassword
Initiates a password reset flow (typically by sending a reset email).Multi-factor Authentication
mfa
Manages Multi-Factor Authentication (MFA) flows, including setup, challenge verification, and removal. TheUser object in the session includes a multiFactor property indicating if MFA is enabled.
Parameters
mfa(params: MfaParams) automatically selects the HTTP verb based on the payload.
| Name | Type | Default | Description |
|---|---|---|---|
method | 'authenticator' | 'email' | 'authenticator' | MFA method to target. |
scope | 'setup' | 'challenge' | 'challenge' | The operation scope. |
token | string | undefined | Challenge token (required for verification and some disable flows). |
code | string | undefined | 6-digit verification code or recovery code. |
remove | boolean | false | If true, disables MFA for the specified method. |
Examples
Error Handling
Non-200 responses may return aChallengeRedirect object ({ url: string }) with an error query parameter. Parse this parameter to display user-friendly error messages.
Utilities
getCsrfToken
Retrieves a CSRF token for form submissions.getProviders
Retrieves the list of configured authentication providers.Advanced Configuration
Custom Authorizer
For advanced use cases, such as connecting to a different API endpoint or managing multiple auth contexts, you can create a custom instance ofAuthorizer.
Constructor Options
| Name | Type | Description |
|---|---|---|
baseUrl | string | The base URL of the API. |
basePath | string | Path to auth endpoints (default: /api). |
init | RequestInit | Default fetch options for all requests. |
state object with baseUrl, session, and loading status. Configuration can be updated later via auth.configure({ ... }).