Desktop and mobile apps
Native apps open the system browser and receive the code on a loopback address or an app scheme (RFC 8252).
Desktop: loopback redirect
- Register
http://127.0.0.1/callback(no port) for your app. - At sign-in, listen on a free port, e.g.
http://127.0.0.1:53117/callback, and use that asredirect_uri. Any port is accepted. - Open the authorize URL (with PKCE,
state,nonce) in the default browser. - When the browser hits your listener, show “You can go back to the game” and exchange the code at the token endpoint with your
code_verifier.
import http from 'node:http'
import { shell } from 'electron'
import { ReanAuth } from '@rean/sdk'
const server = http.createServer()
await new Promise(r => server.listen(0, '127.0.0.1', r))
const redirectUri = `http://127.0.0.1:${server.address().port}/callback`
const auth = new ReanAuth({ clientId: 'rean_xxxxxxxxxxxxxxxxxxxxxxxx', redirectUri })
shell.openExternal(await auth.createSignInUrl())
server.on('request', async (req, res) => {
res.end('Signed in. You can close this tab and go back to the game.')
server.close()
await auth.handleRedirect(new URL(req.url, redirectUri).href)
})Mobile: app scheme
Register a reverse-domain scheme such as com.yourstudio.game:/oauth, open the authorize URL with ASWebAuthenticationSession (iOS) or Custom Tabs (Android), and finish the flow when your app receives the redirect. Libraries like AppAuth handle this for you; point them at the discovery document.
No browser available?
Use the device code flow: the player signs in on their phone or PC instead.