Skip to content

Client API (Vue and React) ​

This is the code in client/src/auth/ of projects generated with --frontend vue|react, or src/selfauth/ after add-frontend. It's a framework-neutral client with a thin Vue or React layer on top. It's your code, and there's no SelfAuth package at runtime.

createAuthClient(options) ​

ts
import { createAuthClient } from "./auth";
const client = createAuthClient({
  baseUrl: "",                 // API origin; "" for same-origin (recommended, via proxy)
  basePath: "/auth",           // must match http.basePath
  csrf: { mode: "header" },    // or "token" (sends the csrfToken from login/me)
  onUnauthenticated: () => {}, // called when the server says the session ended
});
MethodHTTP
register({ email, password }) → { signedIn, user? }POST /auth/register
login({ email, password }) → PublicUserPOST /auth/login
logout(), logoutAll()POST /auth/logout, /auth/logout-all
me() → CurrentUser | nullGET /auth/me (null when signed out)
listSessions(), revokeSession(id)GET /auth/sessions, DELETE /auth/sessions/:id
requestEmailVerification(email), verifyEmail(token)POST /auth/email/verification, /auth/email/verify
changePassword({ currentPassword, newPassword })POST /auth/password/change
requestPasswordReset(email), resetPassword({ token, newPassword })POST /auth/password/reset-request, /auth/password/reset
deleteAccount(currentPassword)POST /auth/account/delete
api(method, path, body?)Any route of your own app, with the same cookie, CSRF and error handling.

Failures throw AuthApiError with a code, which is either a spec error code or NETWORK_ERROR / UNEXPECTED_RESPONSE. describeError(error) turns it into a message for the user. The texts are in messages.ts, so edit them to localize.

Two helpers are worth knowing. safeRedirect(target, fallback) only accepts same-site relative paths, which blocks open redirects (SEC-REDIRECT-001). takeTokenFromFragment() reads #token= and removes it from the address bar.

Vue (vue.ts) ​

ExportUse
createAuthStore(client)Reactive session state: state, isAuthenticated, ready, refresh(), login(), logout(), logoutAll(), can(permission), hasRole(role)
createAuthPlugin(store) / useAuth()Provide and inject the store
installAuthGuard(router, store, { login, forbidden, home })Route meta: requiresAuth, permission, guestOnly
<Can permission="x.y" role="…">Renders its slot only when allowed (with a fallback slot)

React (react.tsx) ​

ExportUse
createAuthStore(client) + <AuthProvider store>Session state via useSyncExternalStore
useAuth(){ status, user, session, isAuthenticated, can, hasRole, store, client }. Sign in and out with store.login(), store.logout() and store.logoutAll()
<RequireAuth permission? loginPath? forbiddenPath?>Redirects signed-out users to login, with a validated return path
<GuestOnly home?>For login and register pages
<Can permission? role? fallback?>Permission-aware rendering

Every frontend check is for the user's benefit only. The server enforces each permission (AUTHZ-CORE-004).

SelfAuth is MIT licensed. Generated code is also offered under 0BSD.