@zod-vault/client
Authentication client with passkeys and email support, plus React hooks.
VaultClient
Section titled “VaultClient”Main client for auth and API access.
import { VaultClient } from "@zod-vault/client";
const client = new VaultClient({ serverUrl: "https://vault.example.com", rpName: "My App", preferPasskey: true, storageKey: "my-auth",});Constructor Options
Section titled “Constructor Options”| Option | Type | Default | Description |
|---|---|---|---|
serverUrl | string | Required | Server URL |
rpName | string | "zod-vault" | Relying party name for passkeys |
preferPasskey | boolean | true | Try passkey before email |
storageKey | string | "zod-vault:auth" | localStorage key for tokens |
Methods
Section titled “Methods”registerEmail
Section titled “registerEmail”const result = await client.registerEmail(email, password);// => { accessToken, refreshToken, user }loginEmail
Section titled “loginEmail”const result = await client.loginEmail(email, password);// => { accessToken, refreshToken, user }registerPasskey
Section titled “registerPasskey”await client.registerPasskey();// Prompts browser for passkey creationloginPasskey
Section titled “loginPasskey”await client.loginPasskey();// Prompts browser for passkeygetToken
Section titled “getToken”const token = client.getToken();// => string | nullgetUser
Section titled “getUser”const user = client.getUser();// => { id: string, email: string } | nullisAuthenticated
Section titled “isAuthenticated”if (client.isAuthenticated()) { // User is logged in}logout
Section titled “logout”await client.logout();refreshToken
Section titled “refreshToken”await client.refreshToken();subscribe
Section titled “subscribe”const unsubscribe = client.subscribe((state) => { console.log(state.isAuthenticated, state.user);});useVaultAuth
Section titled “useVaultAuth”React hook for auth state.
import { useVaultAuth } from "@zod-vault/client";
function Component() { const { isAuthenticated, isLoading, user, error, login, register, logout, } = useVaultAuth(client);}Return Value
Section titled “Return Value”| Property | Type | Description |
|---|---|---|
isAuthenticated | boolean | Whether user is logged in |
isLoading | boolean | Initial auth check in progress |
user | User | null | Current user |
error | Error | null | Last auth error |
login | (email, password) => Promise | Login function |
register | (email, password) => Promise | Register function |
logout | () => Promise | Logout function |
useVaultSync
Section titled “useVaultSync”React hook for sync state.
import { useVaultSync } from "@zod-vault/client";
function Component() { const { status, hasPending, sync, push, pull, } = useVaultSync(useStore);}Return Value
Section titled “Return Value”| Property | Type | Description |
|---|---|---|
status | SyncStatus | Current sync status |
hasPending | boolean | Offline queue has items |
sync | () => Promise | Full sync |
push | () => Promise | Push to server |
pull | () => Promise | Pull from server |
interface User { id: string; email: string;}
interface AuthState { isAuthenticated: boolean; user: User | null; isLoading: boolean; error: Error | null;}
interface AuthResult { accessToken: string; refreshToken: string; user: User;}