Documentation

Sign In a User

Sign in a Davi user with OAuth 2.0 and PKCE, learn who they are from the ID token, and keep their session alive.

This page signs in a Davi user, tells you who they are, and gets an access token that acts on their behalf. It is the same flow for a "Sign in with Davi" button and for an integration that reads the user's data. You need a registered app's Client ID, and its secret if the app is confidential.

The flow spans two hosts. The user's browser signs in on davi.social; your server exchanges and refreshes tokens on api.davi.social.

1. Create a PKCE pair, a state and a nonce

Every client uses PKCE. Generate a random code_verifier and derive its S256 code_challenge (BASE64URL(SHA256(code_verifier))). Generate an unguessable state and nonce for each attempt, and keep all three until the user returns.

function base64url(bytes) {
  return btoa(String.fromCharCode(...new Uint8Array(bytes)))
    .replace(/\+/g, "-")
    .replace(/\//g, "_")
    .replace(/=+$/, "");
}

const random = () => base64url(crypto.getRandomValues(new Uint8Array(32)));

const codeVerifier = random();
const codeChallenge = base64url(
  await crypto.subtle.digest("SHA-256", new TextEncoder().encode(codeVerifier)),
);
const state = random();
const nonce = random();

2. Send the user to davi.social

GET https://davi.social/oauth/authorize
  ?response_type=code
  &client_id=YOUR_CLIENT_ID
  &redirect_uri=https://yourapp.com/callback
  &scope=openid user:read
  &state=STATE
  &nonce=NONCE
  &code_challenge=CODE_CHALLENGE
  &code_challenge_method=S256

openid adds an ID token to the response, which tells you who signed in. user:read lets you read their account. Add whatever else your integration needs from Scopes. The redirect_uri must match one registered on the app exactly.

Davi signs the user in if needed, then shows a consent screen naming your app and the scopes you asked for. If they approved the same scopes before, the screen is skipped.

3. Handle the redirect

On approval, Davi redirects to:

https://yourapp.com/callback?code=AUTHORIZATION_CODE&state=STATE

Check that state matches the one you stored, and refuse the request if it does not.

If the user declines, you receive ?error=access_denied&state=... instead. Treat it as a normal outcome.

4. Exchange the code for tokens

A confidential app authenticates with its secret:

curl -X POST "https://api.davi.social/oauth2/token" \
  -u "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code" \
  -d "code=AUTHORIZATION_CODE" \
  -d "redirect_uri=https://yourapp.com/callback" \
  -d "code_verifier=CODE_VERIFIER"

A public app sends its client_id as a form field instead, with no secret:

curl -X POST "https://api.davi.social/oauth2/token" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=authorization_code" \
  -d "client_id=YOUR_CLIENT_ID" \
  -d "code=AUTHORIZATION_CODE" \
  -d "redirect_uri=https://yourapp.com/callback" \
  -d "code_verifier=CODE_VERIFIER"
{
  "access_token": "eyJhbGciOi...",
  "token_type": "Bearer",
  "expires_in": 3600,
  "refresh_token": "def502...",
  "scope": "openid user:read",
  "id_token": "eyJhbGciOi..."
}

The code is single-use and expires after 10 minutes.

Read scope from the response. A user can only consent to what they can actually do, so a request naming scopes beyond their reach is narrowed rather than refused. scope lists what you were granted.

5. Find out who signed in

Validate the id_token before trusting it. Use a JWT library for your language, configured to:

  1. Verify the RS256 signature against the keys at https://api.davi.social/oauth2/.well-known/jwks.json, selecting by kid.
  2. Check iss equals the issuer in the discovery document, exactly.
  3. Check aud is your client_id.
  4. Check exp has not passed.
  5. Check nonce matches the one you stored in step 1.

The token carries sub, email and email_verified. sub identifies the person's Davi account and never changes, so it is the key for "has this person signed in before".

To call the API about this user, you need their user uuid, which is a different value from sub. Read it, with their name, username and avatar, from /users/me:

curl "https://api.davi.social/api/v1/users/me" \
  -H "Authorization: Bearer ACCESS_TOKEN"

Store uuid beside sub. Every {user_uuid} in a path, every user_uuid in a webhook, and the user_id identifier type take uuid, never sub.

If your app belongs to an organization and requested membership:read, the ID token also carries the user's membership in that organization, so a members-only site can decide at sign-in without another call. See org_membership.

6. Refresh the access token

Access tokens last one hour. Use the refresh token for a new one:

curl -X POST "https://api.davi.social/oauth2/token" \
  -u "YOUR_CLIENT_ID:YOUR_CLIENT_SECRET" \
  -H "Content-Type: application/x-www-form-urlencoded" \
  -d "grant_type=refresh_token" \
  -d "refresh_token=REFRESH_TOKEN"

A public app sends client_id as a form field instead, as in step 4.

Refresh tokens rotate on every use, and replaying a spent one revokes the session. Persist the new refresh_token from each response before using it, and run only one refresh at a time. Two concurrent refreshes, or a retry that re-sends an already-used token, log the user out. The symptom is intermittent logouts.

A refresh token unused for 30 days expires, and the user signs in again.

Signing out

Revoke the refresh token with POST /oauth2/revoke, which ends the session. Users can also revoke your app from their Davi settings, after which its next refresh fails.

Next