Skip to main content

Auth API reference

The Auth API signs up and signs in your app's users at https://<ref>.baas.arianet.app/auth/v1. Its protocol is compatible with the common client libraries (JavaScript, Dart, Swift, Kotlin); if you use a library, it sends these requests for you.

Every request needs the apikey header with the Anon Key or Publishable Key. Requests about the signed-in User also send their token in Authorization: Bearer <access_token>.

BAAS_URL=https://<ref>.baas.arianet.app
ANON_KEY=<ANON_KEY>

Session​

A successful sign-in returns a session:

{
"access_token": "eyJ…",
"token_type": "bearer",
"expires_in": 3600,
"expires_at": 1795000000,
"refresh_token": "…",
"user": {"id": "…", "email": "ali@example.com", "role": "authenticated", "…": "…"}
}
  • access_token is a JWT valid for one hour. Send it in Authorization: Bearer on REST and Storage requests.
  • refresh_token gets a new session and changes on every use; always keep the latest one. Reusing an old refresh token (after 10 seconds) is treated as theft and the whole session is revoked.

Sign up​

POST /auth/v1/signup​

curl -X POST "$BAAS_URL/auth/v1/signup" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"email": "ali@example.com", "password": "a-strong-password", "data": {"display_name": "Ali"}}'
  • With an email or phone ("phone": "+989121234567") and a password. data is free-form user data (user_metadata).
  • With email confirmation on (the default), the User comes back without a session and a confirmation email is sent; the User signs in after clicking the link. With confirmation off, a session is returned at once.
  • The email link returns to ?redirect_to= (if it is in the allowed list) or to the Site URL (Auth settings).
  • Anonymous sign-in: when enabled, POST /auth/v1/signup with no email and password creates an anonymous User with a session.
  • When sign-ups are closed or the Free Plan's MAU limit is reached: 422 with error_code: "signup_disabled".

Password sign-in​

POST /auth/v1/token?grant_type=password​

curl -X POST "$BAAS_URL/auth/v1/token?grant_type=password" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"email": "ali@example.com", "password": "a-strong-password"}'

With a phone: {"phone": "+989121234567", "password": "…"}. Wrong password or address: 400 with invalid_credentials. Unconfirmed email: email_not_confirmed.

One-time code (OTP) and magic link​

POST /auth/v1/otp​

# email: a sign-in link and a 6-digit code are sent
curl -X POST "$BAAS_URL/auth/v1/otp" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"email": "ali@example.com", "create_user": true}'

# phone: an SMS code (only on a Plan with SMS)
curl -X POST "$BAAS_URL/auth/v1/otp" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"phone": "+989121234567"}'

POST /auth/v1/verify​

curl -X POST "$BAAS_URL/auth/v1/verify" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"type": "email", "email": "ali@example.com", "token": "123456"}'
  • type is email for an emailed code and sms for an SMS code. A successful answer is a session.
  • create_user: false only lets existing Users in.
  • Codes and links are valid for 5 minutes and single use (the lifetime and code length can be changed in Auth settings). After 5 wrong codes, the code is revoked.
  • Sending is rate-limited: one message per recipient every 60 seconds (POST /auth/v1/resend sends again).
  • Write phone numbers in international form (+98912… or 98912…); a national number starting with 0 is refused.

Refresh the session​

POST /auth/v1/token?grant_type=refresh_token​

curl -X POST "$BAAS_URL/auth/v1/token?grant_type=refresh_token" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"refresh_token": "<refresh_token>"}'

Refresh before expires_at (client libraries do this automatically).

User details​

GET /auth/v1/user​

curl "$BAAS_URL/auth/v1/user" -H "apikey: $ANON_KEY" -H "Authorization: Bearer $TOKEN"

PUT /auth/v1/user​

Changes data (merged; null removes a key), password, email or phone. A new address gets a confirmation message.

curl -X PUT "$BAAS_URL/auth/v1/user" \
-H "apikey: $ANON_KEY" -H "Authorization: Bearer $TOKEN" -H "Content-Type: application/json" \
-d '{"data": {"display_name": "Ali R."}}'

Sign out​

POST /auth/v1/logout​

curl -X POST "$BAAS_URL/auth/v1/logout?scope=local" \
-H "apikey: $ANON_KEY" -H "Authorization: Bearer $TOKEN"

scope: local (this session), others (all other sessions) or global (all sessions). The answer is 204. The access token stays valid until its one hour runs out but can no longer be refreshed.

Password recovery​

POST /auth/v1/recover​

curl -X POST "$BAAS_URL/auth/v1/recover" \
-H "apikey: $ANON_KEY" -H "Content-Type: application/json" \
-d '{"email": "ali@example.com"}'

A recovery link is emailed. The answer is the same for existing and unknown addresses, so nobody can probe which accounts exist. After signing in through the link, the User sets a new password with PUT /auth/v1/user and {"password": "…"}.

Other paths​

PathPurpose
GET /auth/v1/settingsThe Project's enabled sign-in methods
GET /auth/v1/.well-known/jwks.jsonPublic keys to verify JWT signatures (no key needed)
POST /auth/v1/resendSend the confirmation email or code again
POST /auth/v1/reauthenticateA code to confirm a sensitive action (password change)
/auth/v1/admin/usersUser management from a server; Service Key only (Auth in the panel does the same)

Sign-in with OAuth and OpenID Connect​

After setting up the provider in the panel:

await client.auth.signInWithOAuth({
provider: 'github', // google, gitlab, azure, apple, discord, bitbucket, linkedin_oidc, twitter
options: { redirectTo: 'https://app.example.com/auth/callback' },
})
  • Path: GET /auth/v1/authorize?provider=<name>&redirect_to=<url> sends the User to the provider, which brings them back to /auth/v1/callback. The User then returns to redirect_to with the tokens; redirect_to must be in the allowed redirect URLs.
  • PKCE (recommended, with the code_challenge parameter; the client library does it for you) and implicit (tokens in the URL's #) are both supported.
  • Sign-in with an ID token: a mobile app signed in with Google's or Apple's native SDK turns the ID token into a session with POST /auth/v1/token?grant_type=id_token (in the library: signInWithIdToken).
  • Identity linking: a signed-in User links another account with GET /auth/v1/user/identities/authorize and unlinks one with DELETE /auth/v1/user/identities/{id} (the last identity cannot be unlinked). Linking an account to an anonymous User makes them permanent.
  • A provider that is off answers provider_disabled.

Multi-factor authentication (MFA)​

PathPurpose
POST /auth/v1/factorsEnrol a factor: {"factor_type": "totp"} (the answer has a QR code and secret) or {"factor_type": "phone", "phone": "+98…"}
POST /auth/v1/factors/{id}/challengeStart a verification (for phone, an SMS code is sent)
POST /auth/v1/factors/{id}/verify{"challenge_id", "code"}; on success a new session with aal: "aal2"
DELETE /auth/v1/factors/{id}Remove a factor (needs an aal2 session)

In the client library: client.auth.mfa.enroll, challenge, verify and getAuthenticatorAssuranceLevel. In Policies check (auth.jwt() ->> 'aal') = 'aal2' (example).

CAPTCHA​

When CAPTCHA is on, sign-up, password sign-in, one-time codes and password recovery need the widget's token; in the client library pass it as options.captchaToken. Without it the answer is captcha_failed.

Errors​

{"error_code": "invalid_credentials", "msg": "Invalid login credentials"}

Common codes: validation_failed, invalid_credentials, email_not_confirmed, phone_not_confirmed, user_already_exists, weak_password, signup_disabled, otp_expired, over_email_send_rate_limit, over_sms_send_rate_limit, over_request_rate_limit, refresh_token_already_used, session_not_found, user_banned, phone_provider_disabled. Compatible client libraries read this error_code.

JWT claims​

The access_token is signed with the Project's key (ES256) and carries these claims:

ClaimValue
isshttps://<ref>.baas.arianet.app/auth/v1
audauthenticated
subthe User's id (uuid)
roleauthenticated
exp, iatexpiry (one hour after issue) and issue time
session_idthe session id
email, phonethe User's email and phone
app_metadata, user_metadataapp data (only a server changes it) and user data (the sign-up data)
aal, amrassurance level (aal1) and sign-in method (password, otp, magiclink, …)
is_anonymouswhether the User is anonymous

To verify the signature on your own server, read the public keys from /auth/v1/.well-known/jwks.json. During a key rotation the previous key stays in this list until the overlap ends.

auth.uid() in Policies​

When a request reaches REST or Storage with Authorization: Bearer <access_token>, the token's signature is checked, the request runs as its role (authenticated), and the token's claims are handed to the Database. These functions read them in Policies and Functions:

FunctionValue
auth.uid()the sub claim: the signed-in User's id (uuid); null for anon
auth.role()anon, authenticated or service_role
auth.email()the User's email
auth.jwt()all claims as jsonb

Example: each User sees and writes only their own Rows, and only Users whose app_metadata.role is admin see everything:

alter table public.todos enable row level security;

create policy "own rows" on public.todos
for all to authenticated
using (auth.uid() = user_id)
with check (auth.uid() = user_id);

create policy "admins read all" on public.todos
for select to authenticated
using ((auth.jwt() -> 'app_metadata' ->> 'role') = 'admin');

Your Tables can reference Users: user_id uuid references auth.users (id) on delete cascade. To create a profile Row for every new User automatically, add a Trigger on auth.users with a security definer Function.