Websites and web apps

The authorization code flow with PKCE: send the player to REAN, get a code back, exchange it for tokens.

1. Send the player to REAN

GEThttps://reanstudios.com/oauth/authorizebrowser redirect
NameTypeDescription
client_idrequiredstringYour app's client id.
redirect_urirequiredstringOne of your registered redirect URIs.
response_typerequiredstringAlways code.
scopestringSpace-separated, e.g. openid profile friends.read. Default openid.
staterequiredstringRandom value you store and check when the player comes back (CSRF protection).
code_challengestringbase64url(SHA-256(code_verifier)), 43 characters. Required for public clients.
code_challenge_methodstringS256
noncestringRandom value echoed in the ID token.
promptstringconsent to always show the consent screen; none to fail with consent_required instead of showing it.
import { ReanAuth, localStorageStore } from '@rean/sdk'

const auth = new ReanAuth({
  clientId: 'rean_xxxxxxxxxxxxxxxxxxxxxxxx',
  redirectUri: 'https://yourgame.com/auth/callback',
  scopes: ['openid', 'profile', 'friends.read'],
  storage: localStorageStore(),
})

signInButton.onclick = () => auth.signInWithRedirect()

2. Handle the redirect

The player comes back to your redirect URI with either a code or an error:

text
https://yourgame.com/auth/callback?code=rac_…&state=af0ifjsldkj&iss=https%3A%2F%2Fapi.reanstudios.com
https://yourgame.com/auth/callback?error=access_denied&error_description=The+player+declined.&state=af0ifjsldkj

Check state, then exchange the code within 5 minutes:

POSThttps://api.reanstudios.com/api/oauth/tokenapplication/x-www-form-urlencoded
// on https://yourgame.com/auth/callback
const tokens = await auth.handleRedirect()   // checks state and nonce
console.log(auth.user.preferred_username)
Response
{
  "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6ImF0K2p3dCIs…",
  "token_type": "Bearer",
  "expires_in": 3600,
  "scope": "openid profile friends.read",
  "id_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCIs…"
}

Add offline_access to the scopes to also get a refresh_token. See Tokens and sessions.

3. Verify the ID token (servers)

If your server receives the ID token from the token endpoint itself, over TLS, the response is already trustworthy. If an ID token reaches your server any other way (e.g. sent up by your game client), verify it against REAN's keys:

Node, with the jose package
import { createRemoteJWKSet, jwtVerify } from 'jose'

const JWKS = createRemoteJWKSet(new URL('https://api.reanstudios.com/api/oauth/jwks'))

const { payload } = await jwtVerify(idToken, JWKS, {
  issuer: 'https://api.reanstudios.com',
  audience: 'rean_xxxxxxxxxxxxxxxxxxxxxxxx',
})
// payload.sub is the REAN player id (stable, never reused)