Skip to content

API reference

Greg Bowler edited this page Nov 1, 2026 · 2 revisions

This page summarizes the public API. Start with Quick start guide for complete runtime examples. All core classes are exported from @authwave/typescript-client.

Authenticator

new Authenticator({
  clientKey, currentUriPath,
  authwaveHost: 'login.authwave.com',
  session, redirectHandler, onAuthenticated,
});

clientKey: string and currentUriPath: UriLike are required. Other options are optional; omitting the session raises SessionNotStartedException because there is no native Node.js session global. Construction restores state and processes incoming responses; it may call the callback and throw a redirect signal.

Method Behavior
isLoggedIn(): boolean Whether this instance has a user.
getAuthUser(): AuthUser User or NotLoggedInException.
login(token: Token | null = null): void Stores token and redirects; no-op when logged in.
logout(redirectTo: UriLike | null = null, token: Token | null = null): void Clears authentication before provider logout; explicit destination, configured return, then provider default. Default fake email logs out locally.
setReturnUri(uri: string): void Configures subsequent login and default logout destinations.
getLoginUri(token: Token): BaseProviderUri Builds URI without saving or redirecting.
getAdminUri(): Uri Provider /admin/.
fakeLogin(userId: string, email: string | null = null, redirectTo = '/'): void Development-only synthetic authentication.
getUser(): User Deprecated; emits Node.js DeprecationWarning.

onAuthenticated is (user: AuthUser) => void, synchronous, at most once per exchange under sequential processing. It runs after session mutation and before redirect cleanup.

AuthUser

new AuthUser(id: string, email: string, kvp: Fields = {}) exposes read-only id and email, getData(key): string \| null, and getKvp(): Fields. Fields are copied. User remains a deprecated subclass.

Session and redirect contracts

SessionContainer implements synchronous get(key): unknown, set(key, value): void, contains(key): boolean and remove(key): void. GlobalSessionContainer / SessionArrayWrapper wraps an existing request record and rehydrates JSON session data.

RedirectHandler.redirect(uri: UriLike, code = 303): void throws RedirectSignal, exposing uri and code. Adapters save and send it.

new SessionData(token = null, data = null, returnQuery = null) exposes getToken(), getData(), getReturnQuery(), hasInvokedAuthenticationCallback() and markAuthenticationCallbackInvoked(). Missing token/data throws NotLoggedInException. toJSON()/fromJSON() support server-side persistence. SessionData.STORE_KEY matches PHP's Authwave\SessionData key.

Supporting exchange objects

new Token(secret, sessionIv = new InitVector(), iv = new InitVector()) exposes getIv(), getSecretIv(), generateRequestCipher(message = ''): CipherText, and decode(base64cipher): UserResponseData.

The low-level decoder uses the request IV and accepts uuid or id with a JSON fields array, matching PHP's is_array check. Normal authentication instead uses the session IV and id, email, optional kvp. The reference also requires response kvp to be a JSON array; object-valued kvp raises TypeError in both implementations. Do not substitute the decoder for the main flow.

InitVector exposes getBytes(), immutable-style withBytes() and base64 toString(). CipherText exposes getBytes() and base64 toString(). The implementation uses XSalsa20-Poly1305 secretbox, matching PHP Sodium.

Uri preserves relative paths and raw query strings. It exposes getQuery(), getPath(), getHost(), getScheme(), getPort(), withQuery(), withoutQueryValue() and toString(). UriLike is a string or an object with toString(), including URL.

LoginUri(token, currentPath, deploymentId, baseRemoteUri?), LogoutUri(token, currentPath, deploymentId, baseRemoteUri?, redirectTo?), and AdminUri(baseRemoteUri) extend BaseProviderUri. URI constants and the HTTPS restrictions match PHP.

UserResponseData(id, email, kvp = {}, message = null) exposes getId(), getEmail(), getField() and getAllFields(). Its base BaseResponseData exposes getMessage().

Exceptions

AuthwaveException extends Error. Its subclasses include SessionNotStartedException, NotLoggedInException, InsecureProtocolException, InvalidUserDataSerializationException, and the compatibility declaration ResponseCipherDecryptionException. Cipher helpers expose EncryptionFailureException and DecryptionFailureException. Callback exceptions propagate.

Runtime integrations

  • @authwave/typescript-client/node: createNodeHandler(options, handler) returns a Node.js RequestListener. Handler receives (auth, request) and returns a string/Buffer or promise.
  • @authwave/typescript-client/express: createExpressHandler(options, handler) returns an Express handler. Mount after express-session. Return a body or promise; errors go to next.
  • @authwave/typescript-client/next: createNextHandler(options, handler) returns an App Router Route Handler receiving a standard Request and returning Response. Handler receives (auth, request) and may return a promise. Use runtime = 'nodejs'.

All accept clientKey, optional authwaveHost and synchronous onAuthenticated. Node.js/Next.js additionally require store: SessionStore, and accept cookieName = 'authwave.sid', secureCookie = true, ttlSeconds = 86400. Both export MemorySessionStore for development. Sessions and redirect handlers are provided by the adapters. See Sessions and redirects.

Clone this wiki locally