The example game

Sign-in (OAuth)  9 of 9  Maintained by the Synthcade staff  Updated   5 minutes to read

A working game that signs players in with both flows. Try it here, see what a correct setup does, then download its code and start from it.

The OAuth test game is a small game that signs its players in to its own server with a Synthcade account, in both ways: the in-game prompt and the web flow. It runs here, so you can see what a working setup does before you build yours, and its code is yours to download and start from.

It is plain PHP and JavaScript: no framework, no build step, no database.

What a working setup looks like

Open the game while you are logged in. The page has three columns: the player (the sign-in buttons) and the server check; what the page knows and the error checks; and a log of every call the game makes and every answer it gets. On a narrow screen the log moves below.

  • Sign in with the in-game prompt. A prompt over the game names it and lists what it asks for; Allow can be pressed after three seconds. Then the log shows four calls from the game's server: the token exchange, userinfo, a revoke, and userinfo again, which must answer 401. The player card shows your username and picture.
  • The same, with "Also ask for the email address" ticked. The prompt lists your email address first and marks it. After Allow, the card shows the address too.
  • Not now in the prompt. The log says access_denied. The game does not ask again by itself; pressing the sign-in button again asks again.
  • Sign in with the web flow. A new tab with the consent screen (it cannot be shown inside a frame). After Allow the tab says you are signed in; the game learns it from its own server and shows you signed in.
  • Sign in while logged out. The prompt still appears and asks you to log in to play. The log says not_signed_in.
  • The error checks. For each, what this documentation promises and what came back: invalid_scope, invalid_client and already_pending.

The server check lists everything the game's server needs: PHP and its curl extension, a writable data folder, a client ID and secret, a reachable Synthcade, and the redirect address to register. A red line says what is wrong and how to fix it.

How it is built

  • public/index.php: every endpoint, as index.php?route=…: the page, ping (the server check), start and callback (the web flow), login (the code from the in-game prompt), claim, me and logout.
  • public/lib/oauth.php: the calls to Synthcade: the token exchange with HTTP Basic client authentication, userinfo, revoke, and PKCE.
  • public/lib/signin.php: one sign-in from start to end: exchange the code, ask who it is, start a session.
  • public/lib/store.php: sessions and sign-in attempts as small files in data/.
  • public/app.js: the page: the SDK calls, the web flow's tab, the error checks and the log.
  • public/page.php, style.css, result.php, done.js: the page, its look, and the page the web flow ends on.
  • public/config.example.php: copy it to config.php and fill it in.
  • README.md: how to run it, and what to try.

The two things every game needs are both in it:

  1. The game asks, the page answers with a code. In app.js, the game calls ArcadeSDK.authorize(clientId, ["userinfo"]) and sends the code to its own server (route=login).
  2. The server swaps the code for a token, never the browser. In lib/oauth.php, the server posts the code to /api/oauth/token with its client ID and secret, then asks /api/oauth/userinfo who the token belongs to. The secret never leaves the server, and no token is sent to the browser.

Run your own copy

  1. Upload everything inside public/ to a folder served over https://, for example public_html/my-game/. PHP 8.5 with curl; no Composer, no rewrite rules.
  2. Copy config.example.php to config.php and fill in the site (https://synthcade.net), the address of the folder (app_url, ending in a slash), and the client ID and secret from the next step. Keep config.php out of every repository.
  3. Register an application. As its redirect address, enter exactly what the game shows under Server check.
  4. In your dashboard, publish a game whose address is the folder's. The application and the game must belong to the same account: on a game page, another account's client ID is refused with invalid_client.
  5. Open your game page and try the table above.
Tip

the embed tester shows the in-game prompt's messages without publishing anything. Its prompt hands out made-up codes, which the token endpoint refuses, so finish on a real game page.

Let your AI assistant build it

Point your AI coding assistant at the code and the guides in one address:

https://synthcade.net/docs/oauth/example-game/source.txt

Then ask it for what you need, for example:

Add sign-in with Synthcade to my game, and change nothing else. Read
https://synthcade.net/docs/oauth/example-game/source.txt first: it is a complete,
working example (a small PHP applet) with links to the guides. Take only the
SDK calls and the server exchange from it, not its page, buttons or layout.
My game's server is written in <your language and framework>.

- Start sign-in here, and nowhere else: <where you want it, for example
  "when the game loads" or "when the player opens the leaderboard">.
- Do not add buttons, links, menus, screens or text, and do not change the
  game's look, layout or behaviour in any other way.
- Load the SDK from https://synthcade.net/sdk/v1.js. At that point, call
  ArcadeSDK.supports("oauth"); only when it answers true (the game is running
  in a Synthcade game page that offers sign-in), call
  ArcadeSDK.authorize(<my client ID>, ["userinfo"]). Add "email" only if the
  game really needs it. When supports() answers false or fails, do nothing.
- Send the code to my server; exchange it there at /api/oauth/token with the
  client secret, then read /api/oauth/userinfo. Never put the secret or a
  token in the browser.
- If the player declines or is not signed in (access_denied, not_signed_in,
  already_pending), the game carries on as before. Never ask again on your
  own (no retry, no timer); do ask again each time the player reaches the
  point above again, so a player who said "Not now" can still sign in later.
- If my game has its own username and password fields, leave them as they
  are. When you finish, recommend hiding them while
  ArcadeSDK.supports("oauth") answers true, and say how, but do not make
  that change unless I ask for it.