JavaScript / TypeScriptTested

One dependency-free ES module for browsers, Node 18+, Electron, Deno and Bun. TypeScript types included. This portal signs you in with it.

Install

Download rean-sdk-js-0.1.0.zip and add it to your project (npm install ./rean-sdk-js), or import the single file straight from this site:

js
import { ReanAuth, ReanClient } from 'https://developers.reanstudios.com/sdk/rean-0.1.0.js'

Browser sign-in

js
import { ReanAuth, ReanClient, localStorageStore } from '@rean/sdk'

const auth = new ReanAuth({
  clientId: 'rean_xxxxxxxxxxxxxxxxxxxxxxxx',
  redirectUri: `${location.origin}/callback`,
  scopes: ['openid', 'profile', 'offline_access', 'friends.read', 'presence.read'],
  storage: localStorageStore(),
})

if (location.pathname === '/callback') {
  await auth.handleRedirect()          // checks state + nonce, stores tokens
  history.replaceState(null, '', '/')
}
if (!auth.signedIn) signInButton.onclick = () => auth.signInWithRedirect()

const rean = new ReanClient({ auth })
const me = await rean.me()
const friends = await rean.friends()

Games and tools (Node / Electron)

js
const auth = new ReanAuth({ clientId: 'rean_xxxxxxxxxxxxxxxxxxxxxxxx' })

if (!(await auth.signInWithLauncher())) {        // -ReanAuthCode from process.argv
  const device = await auth.startDeviceSignIn()
  console.log(`Go to ${device.verificationUri} and enter ${device.userCode}`)
  await device.poll()
}

const rean = new ReanClient({ auth })
const presence = rean.keepActivity({ details: 'Lobby', partyId, partySize: 1, partyMax: 4, joinable: true })
const stop = rean.onEvent(async e => {
  if (e.type === 'invite.received') joinLobby((await rean.respondToInvite(e.data.id, true)).joinSecret)
})
// on exit:
await presence.stop()
stop()

API summary

ReanAuth
createSignInUrl()Authorize URL with PKCE, state, nonce.
signInWithRedirect() / handleRedirect(url)Browser flow.
startDeviceSignIn()Returns { userCode, verificationUri, poll(), cancel() }.
signInWithLauncher(code?)Launcher code from argv.
getAccessToken() / refresh()Auto-refresh one minute before expiry.
signOut()Clears and revokes.
onChange(fn)Token changes (save rotated refresh tokens).
ReanClient
me()The signed-in player.
friends() / presence(ids)Friends and presence.
setActivity() / keepActivity() / clearActivity()Rich presence.
invites() / sendInvite() / respondToInvite()Invites.
events(cursor) / onEvent(fn)Event feed.

Errors are ReanError with status and code.