Authentication
End-user auth with anonymous sessions, Apple / Google social login, email-password, and account linking — all backed by Amba-native JWTs.
Amba provides a complete end-user auth system for mobile apps. Every user starts with an anonymous identity stored client-side; they can later sign in with Apple, Google, or email / password, and Amba preserves their anonymous history through account linking.
Credentials live in your project's database (physically isolated from every other Amba project) and passwords are hashed with bcrypt — no DIY crypto, no third-party auth vendor to get locked into.
How it works
- On first launch,
init()generates a localanonymous_idand persists it to storage. - When the user signs in (Apple, Google, or email), the SDK exchanges a credential for a session token (short-lived JWT) + refresh token (90 days). Both are persisted.
- The SDK attaches
Authorization: Bearer <session_token>to every user-scoped request. - When the session expires, the SDK rotates via
POST /client/auth/refresh(rotating both tokens and revoking the old refresh token's server-side record). logout()revokes the refresh token server-side and clears local state.
JWT claims
Session tokens carry:
| Claim | Meaning |
|---|---|
sub | app_users.id |
pid | projectId — defense in depth: the API rejects a token that doesn't match the API key's project |
anon | the original anonymous_id |
Refresh tokens additionally carry a sid (session id) that maps to a server-side app_user_sessions row. Rotation revokes the old row and mints a new one, so a replayed refresh token is detected.
SDK usage
The @amba/client auth module signatures:
Anonymous identity (automatic)
Email signup / login
Passwords are hashed server-side with bcrypt at cost factor 10. The plaintext password is never stored and never returned to the client.
Apple Sign In
The server verifies the token's signature + issuer against Apple's JWKS before trusting any claims.
Google Sign In
Same JWKS verification on the server.
Account linking
Preserve a user's anonymous history when they later add a social login:
Email linking goes through the verified signUpWithEmail / loginWithEmail flow, not linkAccount.
Session management
Session shape:
Token refresh
The SDK persists refreshToken on persistSession(). Apps typically never call refresh manually — the API retries with exponential back-off, and a stale session token should be swapped for a refreshed one via POST /client/auth/refresh { refresh_token }.
If you need to force-rotate (e.g. after a permissions change on the server), call client.auth.refresh():
Current user
client.auth.me() fetches /client/users/me using the current session token and returns the AppUser record:
Common AmbaApiError.code values on auth
| Code | When |
|---|---|
INVALID_CREDENTIALS | Wrong email/password on loginWithEmail. |
USER_EXISTS | Email already registered on signUpWithEmail. |
INVALID_EMAIL | Malformed email. |
WEAK_PASSWORD | Password doesn't meet policy. |
RATE_LIMITED | Too many attempts from this IP / user. |
INVALID_TOKEN | Apple/Google token failed signature verification. |
Switch on err.code rather than err.message — messages are human-readable and may change.
Expo one-liners
@amba/expo wraps the native sign-in flows so you don't have to touch Apple / Google SDKs directly:
For email auth just use Amba.auth.signUpWithEmail(...) / Amba.auth.loginWithEmail(...) — the auth accessor is passed through to the underlying AmbaClient.
Routes reference
| Method | Path | Description |
|---|---|---|
POST | /client/auth/anonymous | Create a new anonymous user + session |
POST | /client/auth/social | Exchange an Apple or Google identity token for an Amba session |
POST | /client/auth/email/signup | Email signup (bcrypt-hashed password) |
POST | /client/auth/email/login | Email login (verifies bcrypt hash) |
POST | /client/auth/link | Link the current anonymous session to a social provider |
POST | /client/auth/refresh | Rotate session + refresh tokens |
POST | /client/auth/logout | Revoke the refresh token |
All client auth routes require X-Api-Key. /link, /refresh, and /logout additionally require the current session / refresh token in the body.
Database tables
| Table | Purpose |
|---|---|
app_users | Core user record: id, email, anonymous_id, display_name, auth_providers (JSONB), properties (JSONB), password_hash, timestamps |
account_links | Audit log of anonymous → authenticated transitions |
app_user_sessions | Server-side refresh-token records. Each carries a sha256 hash of the refresh token, expires_at, revoked_at, user_agent, and ip |