Skip to content

@zod-vault/client

Authentication client with passkeys and email support, plus React hooks.

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",
});
OptionTypeDefaultDescription
serverUrlstringRequiredServer URL
rpNamestring"zod-vault"Relying party name for passkeys
preferPasskeybooleantrueTry passkey before email
storageKeystring"zod-vault:auth"localStorage key for tokens
const result = await client.registerEmail(email, password);
// => { accessToken, refreshToken, user }
const result = await client.loginEmail(email, password);
// => { accessToken, refreshToken, user }
await client.registerPasskey();
// Prompts browser for passkey creation
await client.loginPasskey();
// Prompts browser for passkey
const token = client.getToken();
// => string | null
const user = client.getUser();
// => { id: string, email: string } | null
if (client.isAuthenticated()) {
// User is logged in
}
await client.logout();
await client.refreshToken();
const unsubscribe = client.subscribe((state) => {
console.log(state.isAuthenticated, state.user);
});

React hook for auth state.

import { useVaultAuth } from "@zod-vault/client";
function Component() {
const {
isAuthenticated,
isLoading,
user,
error,
login,
register,
logout,
} = useVaultAuth(client);
}
PropertyTypeDescription
isAuthenticatedbooleanWhether user is logged in
isLoadingbooleanInitial auth check in progress
userUser | nullCurrent user
errorError | nullLast auth error
login(email, password) => PromiseLogin function
register(email, password) => PromiseRegister function
logout() => PromiseLogout function

React hook for sync state.

import { useVaultSync } from "@zod-vault/client";
function Component() {
const {
status,
hasPending,
sync,
push,
pull,
} = useVaultSync(useStore);
}
PropertyTypeDescription
statusSyncStatusCurrent sync status
hasPendingbooleanOffline queue has items
sync() => PromiseFull sync
push() => PromisePush to server
pull() => PromisePull 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;
}