/** * Hawcx SDK Types * * Defines client-side state types and re-exports protocol types. */ import type { AuthError } from "./errors"; export type { AuthError } from "./errors"; export { AuthErrorCategory } from "./errors"; export interface AuthNotice { code: string; message: string; } export { PROTOCOL_VERSION, type WireRequest, type WireResponse, type ResponseMeta, type Action, type StartAction, type SelectMethodAction, type SubmitCodeAction, type SubmitTotpAction, type SubmitPhoneAction, type SubmitSignatureAction, type OAuthCallbackAction, type ResendAction, type PollAction, type CancelAction, type AuthStep, type SelectMethodStep, type EnterCodeStep, type EnterTotpStep, type SetupTotpStep, type SetupSmsStep, type DeviceChallengeStep, type RedirectStep, type AwaitApprovalStep, type CompletedStep, type ErrorStep, type ErrorAction, type ErrorDetails, type Method, type DeviceInfo, type StepType, isSelectMethodStep, isEnterCodeStep, isEnterTotpStep, isSetupTotpStep, isSetupSmsStep, isDeviceChallengeStep, isRedirectStep, isAwaitApprovalStep, isCompletedStep, isErrorStep, } from "./protocol"; export type AuthMode = "signin" | "signup" | "account_manage"; export interface AuthResult { authCode: string; expiresAt: string; /** PKCE code verifier for OAuth 2.1 token exchange (optional) */ codeVerifier?: string; /** * hx_auth session JWT — pass to `logoutEverywhere(bearer)` and other * self-service device calls. Present only when the server minted one. */ sessionToken?: string; } export interface AuthStateIdle { status: "idle"; } export interface AuthStateLoading { status: "loading"; session?: string; notice?: AuthNotice; } export interface AuthStateStep { status: "step"; session: string; /** The server's auth step - what the user needs to do */ step: import("./protocol").AuthStep; notice?: AuthNotice; } export interface AuthStateCompleted { status: "completed"; session: string; authCode: string; expiresAt: string; /** hx_auth session JWT (bearer for self-service device calls), if minted. */ sessionToken?: string; } export interface AuthStateError { status: "error"; session?: string; error: AuthError; /** Previous step preserved for retryable errors (e.g. invalid TOTP/backup code) */ previousStep?: import("./protocol").AuthStep; } export type AuthState = AuthStateIdle | AuthStateLoading | AuthStateStep | AuthStateCompleted | AuthStateError; /** Check if state is idle */ export declare function isIdle(state: AuthState): state is AuthStateIdle; /** Check if state is loading */ export declare function isLoading(state: AuthState): state is AuthStateLoading; /** Check if state has an active step (user action needed) */ export declare function isAuthStep(state: AuthState): state is AuthStateStep; /** Check if state is completed (auth successful) */ export declare function isCompleted(state: AuthState): state is AuthStateCompleted; /** Check if state is error */ export declare function isError(state: AuthState): state is AuthStateError; /** Get step type from state, or null if not in step state */ export declare function getStepType(state: AuthState): import("./protocol").StepType | null; /** Get methods from state (for method selection), or empty array */ export declare function getMethods(state: AuthState): import("./protocol").Method[]; /** Get error from state, or null if not in error state */ export declare function getAuthError(state: AuthState): AuthError | null; /** * Get the server-driven notice from state, or null if none. * Notices are non-fatal messages (e.g. `device_revoked`) that ride along with * a step or loading state. */ export declare function getAuthNotice(state: AuthState): AuthNotice | null; export interface StorageAdapter { getItem(key: string): string | null; setItem(key: string, value: string): void; removeItem(key: string): void; } export interface Logger { debug?: (message: string, data?: unknown) => void; info?: (message: string, data?: unknown) => void; warn?: (message: string, data?: unknown) => void; error?: (message: string, data?: unknown) => void; } export declare const noopLogger: Logger; export declare const consoleLogger: Logger; export interface AuthConfig { /** Your Hawcx configuration ID (API key) */ configId: string; /** API base URL (default: https://api.hawcx.com/v1) */ apiBase?: string; /** Logger instance for debugging */ logger?: Logger; /** Custom fetch implementation (for testing) */ fetch?: typeof fetch; /** Request timeout in milliseconds (default: 10000) */ timeout?: number; /** * OIDC-standard, space-delimited scope string the relying party wants to * request (e.g. "openid profile email phone address"). Optional. When unset, * the SDK omits `scope` from the wire "start" request entirely, so the wire * shape is unchanged from a client that never set it. */ scope?: string; } export interface SignOutOptions { /** * Also remove this browser's device credentials for the signed-in * identifier (see HawcxAuth.forgetDevice). Default: false. */ forgetDevice?: boolean; /** * Identifier whose credentials `forgetDevice` removes. Defaults to the * identifier that signed in on this client; pass it explicitly when the * client was re-created since (e.g. after a page reload or redirect). */ identifier?: string; } export interface HawcxAuth { /** * Send an action to the server. * This is the primary method - all auth operations go through here. */ send(action: import("./protocol").Action): Promise; /** * Convenience: Start an authentication flow. */ start(identifier: string, flowType?: AuthMode, device?: import("./protocol").DeviceInfo): Promise; /** * Convenience: Select an authentication method. */ selectMethod(methodId: string): Promise; /** * Convenience: Submit an OTP code. */ submitCode(code: string): Promise; /** * Convenience: Submit a TOTP code. */ submitTotp(code: string): Promise; /** * Convenience: Submit a phone number for SMS OTP enrollment. */ submitPhone(phone: string): Promise; /** * Convenience: Request code resend. */ resend(): Promise; /** * Convenience: Cancel the current flow. */ cancel(): Promise; /** * Get current auth state. */ getState(): AuthState; /** * Get auth completion result (null if not authenticated). */ getCompletion(): AuthResult | null; /** * Get current session ID (for multi-step flows). */ getSessionId(): string | undefined; /** * Adopt an existing flow session (e.g. after a full-page IdP redirect * round-trip). The next send() continues that flow. Pass `identifier` to * enable automatic device-enrollment handling on the resumed leg — without * it, setup_device / device_challenge steps fail (crypto storage is keyed * by identifier and wire steps don't carry it). */ resumeSession(session: string, identifier?: string): void; /** * PKCE verifier generated at start() — needed to redeem the auth code * when the flow survives a page navigation. */ getPkceVerifier(): string | null; /** * Reset to idle state. */ reset(): void; /** * Sign out and clear session (local only). Device credentials are kept, so * the next sign-in for the same identifier on this browser stays silent. */ signOut(): void; /** * Sign out with options. With `{ forgetDevice: true }`, also removes this * browser's device credentials for the signed-in identifier (or * `options.identifier`; see forgetDevice). The session is cleared * synchronously, as with signOut(); completion listeners are notified and * the returned promise settles once the credentials are deleted. It rejects * if they could not be, or no identifier is known. */ signOut(options: SignOutOptions): Promise; signOut(options?: SignOutOptions): void | Promise; /** * "Forget this device": delete the device record and UAK key pair stored in * this browser for `identifier` (default: the identifier that signed in on * this client, else the flow in progress), so the next sign-in with that * identifier cannot complete device trust silently and must verify again. * If it targets a flow still in progress, that flow is ended (client reset * to idle). A completed sign-in is NOT ended: this is a local storage wipe, * not a sign-out — use signOut({ forgetDevice: true }) to do both. * * Call it on shared or public computers at sign-out, or when the user * explicitly chooses "remove this device". It only affects local storage; * it does not revoke the device server-side. * * Rejects if no identifier is given and none is known (e.g. after a page * reload — pass the identifier then), or if storage deletion fails. */ forgetDevice(identifier?: string): Promise; /** * Sign out from ALL of the user's sessions (incl. this one) and trigger OIDC * logout-everywhere, WITHOUT de-enrolling any device — every device stays * trusted, so the next sign-in is silent (no OTP re-enroll). Pass the caller's * hx_auth session bearer token. On success, resets to idle but PRESERVES local * device credentials. On failure, sets error state and throws an Error * carrying the structured `code`/`category`. */ logoutEverywhere(bearerToken: string): Promise<{ sessionsEnded: number; }>; /** * Subscribe to state changes. */ onStateChange(listener: (state: AuthState) => void): () => void; /** * Subscribe to auth completion. */ onCompletion(listener: (result: AuthResult | null) => void): () => void; /** * Get configuration ID. */ getConfigId(): string; /** * Check if device has stored credentials for identifier. */ hasDeviceCredentials(identifier: string): Promise; }