Skip to content

Setup

Create a client instance by choosing a token mode. Cookie mode is recommended for web apps, bearer mode for mobile or non-browser environments.

import { createAuthClient } from 'authfort-client';

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

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

const auth = createAuthClient({
  baseUrl: '/auth',
  tokenMode: 'bearer',
  tokenStorage: {
    get: async () => localStorage.getItem('refresh_token'),
    set: async (token) => localStorage.setItem('refresh_token', token),
    clear: async () => localStorage.removeItem('refresh_token'),
  },
});
import { createAuthClient } from 'authfort-client';

const auth = createAuthClient({
  baseUrl: '/auth',
  tokenMode: 'bearer',
  tokenStorage: {
    get: async () => localStorage.getItem('refresh_token'),
    set: async (token) => localStorage.setItem('refresh_token', token),
    clear: async () => localStorage.removeItem('refresh_token'),
  },
});
Option Type Default Description
baseUrl string — (required) Auth server URL (e.g., /auth or https://api.example.com/auth)
tokenMode 'cookie' | 'bearer' 'cookie' How tokens are delivered
refreshBuffer number 30 Seconds before expiry to trigger refresh
tokenStorage TokenStorage Required for bearer mode

Call initialize() once at app startup to check for an existing session:

await auth.initialize();
await auth.initialize();

This attempts to restore the user’s auth state:

  • Cookie mode — calls /auth/me to check if session cookies are valid
  • Bearer mode — reads the stored refresh token and refreshes the access token

After initialization, the auth state is either 'authenticated' or 'unauthenticated'.

Create the client once and share it across your app:

src/auth.ts
import { createAuthClient } from 'authfort-client';

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

export const auth = createAuthClient({
  baseUrl: '/auth',
  tokenMode: 'cookie',
});
src/main.ts
import { auth } from './auth';

await auth.initialize();
import { auth } from './auth';

await auth.initialize();