User Authentication

The PlayAI SDK requires user authentication for its operation. The process of getting a session token for the user is facilitated through the API key and game ID provided to the game. It is imperative to note that this operation should be executed on the game's backend servers to prevent exposure of the API key on the client side.

The type field in the request body can be one of the following: google, twitter, evm, or solana. The account field should contain the user's account details.

The account field is a string that represents the user's account details. For Google and Twitter, it can be the user's email or username respectively. For EVM and Solana, it should be the user's wallet address

Here's an example of how you can get a session token on your server:

const res = await fetch("https://api.playai.network/sdk/your_game_id/session/create", {
  method: "POST",
  headers: {
    "Content-Type": "application/json",
    "x-playai-token": "your_api_key",
  },
  body: JSON.stringify({ type: "google", account: "user@google.com" })
});

await res.json();

return res.sessionToken;

Once the session token is obtained, it can be used on the client side to authenticate the user with the PlayAI SDK:

playAI.loginWithSessionToken(sessionToken);

The authentication process should start by calling the getCurrentSession method, which returns the session details. If the user is not authenticated, getCurrentSession will return null.

Here's an example of how you can use the session token to authenticate the user:

const currentSession = await playAI.getCurrentSession();
if (!currentSession) {
  const sessionToken = await fetch("<your_game_server_api>");
  await playAi.loginWithSessionToken(sessionToken);
} 

Last updated