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.
Cookie Mode
Section titled “Cookie Mode”import { createAuthClient } from 'authfort-client';
const auth = createAuthClient({
baseUrl: '/auth',
tokenMode: 'cookie',
}); import { createAuthClient } from 'authfort-client';
const auth = createAuthClient({
baseUrl: '/auth',
tokenMode: 'cookie',
}); Bearer Mode
Section titled “Bearer Mode”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'),
},
}); Configuration
Section titled “Configuration”| 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 |
Initialize
Section titled “Initialize”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/meto 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'.
App Integration
Section titled “App Integration”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();