Installation

@niledatabase/client exposes the the client side functions for various authorization requests. It handles API requests for sign-in, sign-up, sessions, and password reset. The client is designed to work with @niledatabase/server which provides the server-side routes.
npm install @niledatabase/client

Getting Started

For most cases, These helpers call the underlying singleton instance of Authorizer, which can be accessed via auth
import {
  auth
  // signIn // could just use this instead
} from '@niledatabase/client';

await auth.signIn('credentials', { email: 'user@example.com', password: 'pw' });

getSession

Fetches the current session. If a session is cached and still valid it returns that, otherwise it requests /auth/session.
await auth.getSession();

signIn

Sends a sign-in request to the authentication endpoint. The initial signIn values are the supported providers for single sign on, in addition to credentials
await auth.signIn('credentials', {
  email: 'user@example.com',
  password: 'password',
});

Params

NameTypeDescription
callbackUrlstringThe URL to return when the request is complete.
redirectbooleanAllows configuration of client-side redirects in the case of credential login

signOut

Logs the user out and clears the session.
await auth.signOut({ callbackUrl: '/goodbye' });

signUp

Creates a new user account. You can also create tenants during sign up. A boolean value will create a tenant with the same email address. If a string is passed, that will be the name of the created tenant.
await auth.signUp({
  email: 'user@example.com',
  password: 'pass',
  createTenant: true,
});

resetPassword

Sends requests to the password reset endpoints. You must be an authorized user to do this action.
await auth.resetPassword({
  email: 'user@example.com',
  password: 'new-pass',
});

getCsrfToken

Returns a CSRF token used for form submissions.
const token = await auth.getCsrfToken();

getProviders

Returns a list of available authentication providers.
const providers = await auth.getProviders();

Authorizer

You can also customize your own authorizer on the client by creating an instance of Authorizer.
import { Authorizer } from '@niledatabase/client';

const auth = new Authorizer({ baseUrl: 'https://myapp.com' });

Constructor Options

new Authorizer(config) accepts the following options:
NameTypeDescription
baseUrlstringThe base URL of your application.
basePathstringOptional API path, defaults to /api.
initRequestInitDefault fetch options for all requests.
The instance exposes a state object with baseUrl, session, and loading status. You can update the configuration later by calling auth.configure({ baseUrl }).