Skip to content

Client API

Factory function that creates an AuthClient instance.

import { createAuthClient } from 'authfort-client';

const auth = createAuthClient({
  baseUrl: '/auth',
  tokenMode: 'cookie',
});
import { createAuthClient } from 'authfort-client';

const auth = createAuthClient({
  baseUrl: '/auth',
  tokenMode: 'cookie',
});
FieldTypeDefaultDescription
baseUrlstring— (required)Auth server base URL
tokenMode'cookie' | 'bearer''cookie'Token delivery mode
refreshBuffernumber30Seconds before expiry to trigger refresh
tokenStorageTokenStorageRequired for bearer mode
interface TokenStorage {
  get(): Promise<string | null>;
  set(token: string): Promise<void>;
  clear(): Promise<void>;
}
interface TokenStorage {
  get(): Promise<string | null>;
  set(token: string): Promise<void>;
  clear(): Promise<void>;
}

Check for an existing session on startup.

Returns: Promise<void>

Create a new user account.

ParamTypeDescription
data.emailstringUser email
data.passwordstringPassword
data.namestring?Display name
data.avatarUrlstring?Profile image URL
data.phonestring?Phone number

Returns: Promise<AuthUser> Throws: AuthClientError

Authenticate with email and password.

ParamTypeDescription
data.emailstringUser email
data.passwordstringPassword

Returns: Promise<AuthUser> Throws: AuthClientError

Start OAuth flow. In redirect mode (default), navigates the browser. In popup mode, opens a popup window and returns a promise.

ParamTypeDescription
providerOAuthProviderProvider name — built-in ('google', 'github') or any generic provider string
optionsOAuthSignInOptions?OAuth flow options

Returns: void (redirect mode) or Promise<AuthUser> (popup mode)

Revoke the refresh token and clear auth state.

Returns: Promise<void>

Get the current user from the server.

Returns: Promise<AuthUser> Throws: AuthClientError if not authenticated

Get a valid access token. Refreshes automatically if expired.

Returns: Promise<string | null>null in cookie mode

Make an authenticated request. Retries once on 401.

ParamTypeDescription
urlstringRequest URL
optionsRequestInit?Standard fetch options

Returns: Promise<Response>

Listen for auth state changes.

ParamTypeDescription
callback(state: AuthState, user: AuthUser | null) => voidState change handler

Returns: () => void — unsubscribe function

Request a magic link for passwordless login.

ParamTypeDescription
emailstringUser email

Returns: Promise<void> Throws: AuthClientError

Verify a magic link token and log in.

ParamTypeDescription
tokenstringMagic link token

Returns: Promise<AuthUser> Throws: AuthClientError

Request an email OTP code for passwordless login.

ParamTypeDescription
emailstringUser email

Returns: Promise<void> Throws: AuthClientError

Verify an email OTP code and log in.

ParamTypeDescription
emailstringUser email
codestring6-digit OTP code

Returns: Promise<AuthUser> Throws: AuthClientError

Verify email address with a verification token.

ParamTypeDescription
tokenstringVerification token

Returns: Promise<void> Throws: AuthClientError


interface AuthUser {
  id: string;
  email: string;
  name?: string;
  phone?: string;
  roles: string[];
  emailVerified: boolean;
  avatarUrl?: string;
  createdAt: string;
}
interface AuthUser {
  id: string;
  email: string;
  name?: string;
  phone?: string;
  roles: string[];
  emailVerified: boolean;
  avatarUrl?: string;
  createdAt: string;
}
type AuthState = 'authenticated' | 'unauthenticated' | 'loading';
type AuthState = 'authenticated' | 'unauthenticated' | 'loading';
type OAuthProvider = 'google' | 'github' | (string & {});
type OAuthProvider = 'google' | 'github' | (string & {});
interface OAuthSignInOptions {
  /** OAuth flow mode. 'redirect' (default) or 'popup'. */
  mode?: 'redirect' | 'popup';
  /** URL path to redirect to after auth (relative path starting with "/"). Only used in redirect mode. */
  redirectTo?: string;
}
interface OAuthSignInOptions {
  /** OAuth flow mode. 'redirect' (default) or 'popup'. */
  mode?: 'redirect' | 'popup';
  /** URL path to redirect to after auth (relative path starting with "/"). Only used in redirect mode. */
  redirectTo?: string;
}
class AuthClientError extends Error {
  message: string;
  code: string;
  statusCode: number;
}
class AuthClientError extends Error {
  message: string;
  code: string;
  statusCode: number;
}

import { AuthProvider, useAuth } from 'authfort-client/react';
import { AuthProvider, useAuth } from 'authfort-client/react';
ExportDescription
AuthProviderContext provider component. Props: client: AuthClient. Auto-calls initialize().
useAuth()Hook returning { state, user, isAuthenticated, isLoading, client }
import { provideAuth, useAuth } from 'authfort-client/vue';
import { provideAuth, useAuth } from 'authfort-client/vue';
ExportDescription
provideAuth(client)Provide auth to component tree. Auto-calls initialize().
useAuth()Composable returning { state, user, isAuthenticated, isLoading, client }
import { createAuthStore } from 'authfort-client/svelte';
import { createAuthStore } from 'authfort-client/svelte';
ExportDescription
createAuthStore(client)Returns { state, user, isAuthenticated, isLoading, client } as Svelte stores. Auto-calls initialize().