Skip to main content

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 the Authorizer 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

Options:

signUp

Creates a new user account. Can optionally create a new tenant for the user.

Parameters

Options:

signOut

Logs the current user out and clears the session.

Parameters

Options:

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. The User 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.

Examples

Error Handling

Non-200 responses may return a ChallengeRedirect 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 of Authorizer.

Constructor Options

The instance exposes a state object with baseUrl, session, and loading status. Configuration can be updated later via auth.configure({ ... }).