AuthClient
client.auth handles registration, login/logout, and session persistence.
register(options)
Section titled “register(options)”await client.auth.register({ username, password, email, // optional profile, // optional inviteCode, // optional -- required if the server has registration closed acknowledgedRules,})Posts to POST /register (see the REST API docs for server-side validation rules -- username format, invite-code handling, email-verification gating, etc.). On success, stores the returned token and user and returns { user, token }. Throws AuthenticationError if username/password are missing from the call, or if the server's response doesn't include a token.
login(options)
Section titled “login(options)”await client.auth.login({ username, password })// orawait client.auth.login({ id: '@alice@kwln.social', password })Accepts either a bare username or a full id (@user@domain) -- matches the server's own POST /auth/login body shape.
logout()
Section titled “logout()”await client.auth.logout()Clears the in-memory user/token and removes the persisted token from storage.
restoreSession()
Section titled “restoreSession()”const user = await client.auth.restoreSession() // null if no valid sessionCalled automatically by client.init(). This is a two-step process, not a single trust-the-token read:
- Local decode. The client decodes the stored JWT itself (no signature verification client-side -- that's the server's job) to get an initial
_usersnapshot immediately, with no network round trip. - Server refresh. It then calls
GET /auth/meto refresh the fields that are not present in the JWT payload:profile,prefs,isServerAdmin,following,allFollowing,blocked,muted,groups. This refresh is best-effort -- if it fails (offline, server down),restoreSession()falls back to the locally-decoded snapshot rather than failing outright.
If the token is missing, malformed, or has no user.id, restoreSession() logs the user out and returns null.
_decodeToken handles base64url-to-base64 normalization manually (JWTs are base64url-encoded, not standard base64) and throws AuthenticationError('Failed to decode token') on a malformed token.
Reading the current session
Section titled “Reading the current session”client.auth.getUser() // sync -- returns the cached user object, or nullawait client.auth.getToken() // async -- reads from storageawait client.auth.isAuthenticated() // asyncgetUser() is synchronous because it just returns the in-memory cached value set by login()/register()/restoreSession() -- it does not hit the network or storage. getToken() and isAuthenticated() are async because they read through the storage adapter.