Skip to content
exwave.Docs

Games & player access tokens

Your game can use the same accounts as your website. When a player connects to one of your game servers, the game sends a player access token (PAT). The game server checks the token, without calling Exwave, and knows which player is connecting.

A PAT lasts 15 minutes at most. Don’t store it: ask for a new one each time the game connects.

How the game gets a PAT depends on where it runs:

In React, with Next.js or Vite, get the PAT from the client in useExwave():

import { useExwave } from "@exwave-nexus/auth-react";
export function PlayButton() {
const { auth } = useExwave();
async function play() {
const token = await auth.getPlayerAccessToken(); // null when signed out
if (!token) return;
// Send the token to your game server, for example when the player joins a match.
await fetch("https://game.example.com/join", {
method: "POST",
headers: { Authorization: `Bearer ${token}` },
});
}
return <button onClick={play}>Play</button>;
}

The token is valid for at least one more minute when you get it. With Next.js, the client asks your own /api/exwave/token route for it. Without React, you can call that route yourself. It answers with access_token, the PAT, and expires_in, its lifetime in seconds:

const response = await fetch("/api/exwave/token");
const { access_token, expires_in } = await response.json();

/api/exwave/token only answers requests from your own site.

From an engine or launcher (OpenID Connect)

Section titled “From an engine or launcher (OpenID Connect)”

A game that runs outside the browser signs players in on Exwave’s hosted sign-in pages, with OpenID Connect (OIDC). Your game is a public client: it has a client ID but no secret, because anything you ship in a game can be read.

In Nexus → Auth, the Use in a game or launcher section shows what you need: the Issuer, Client ID, Scopes, Platform ID and Token exchange URL.

  1. Allow your game’s callback address.

    The hosted sign-in page sends the player back to your game at a callback address, such as mygame://callback. Add it to the Redirect URLs in Nexus → Auth → Settings.

  2. Sign the player in.

    Open the sign-in page with the Authorization Code flow with PKCE, using the issuer, client ID and scopes from Nexus. An OpenID Connect library for your engine can do this for you. When the player has signed in, your game gets an ID token.

  3. Exchange the ID token for a PAT.

    Within 10 minutes of getting the ID token, send it to the token exchange URL from Nexus. project_id is your platform ID:

    POST <token exchange URL>
    Content-Type: application/json
    { "project_id": "<platform ID>", "id_token": "<ID token>" }

    The answer has access_token, the PAT, and expires_in, its lifetime in seconds.

Your game server checks each PAT with verifyPlayerAccessToken, from @exwave-nexus/auth/server:

import { verifyPlayerAccessToken } from "@exwave-nexus/auth/server";
const { userId, claims } = await verifyPlayerAccessToken(token, {
publishableKey: "exw_pk_...",
});

It checks that the token:

  • is signed with your platform’s keys, which it downloads once and then keeps
  • was issued by Exwave Auth, for your platform
  • hasn’t expired

If any check fails, it throws an ExwaveAuthError with the code invalid_token. Turn the player away when that happens.