User
The User module provides the functionality to obtain information and Token
of the current logged-in player. If the game allows access to all features without logging in, this module is not required; otherwise, you need to call the relevant interfaces of this module to implement the corresponding functions.
After reading the part related to your game engine on the JOGOS_SDK Introduction
page, you can use the relevant functions as follows:
window.JOGOS_SDK.user;
Get Current User Information
You can use the following method to retrieve information about the current player:
try {
const user = await window.JOGOS_SDK.user.getUser();
console.log('Get user result', user);
} catch (err) {
// Handle exceptions
console.error(err);
}
If the current player is not registered or logged in, the system will automatically open the login/registration dialog box, requiring the user to register or log in successfully to continue the game; otherwise, it will return a 401 Unauthorized, please log in again
exception.
If the current player's account has been frozen or canceled, the corresponding exception information will also be returned.
The structure of the successfully returned player object is as follows:
// User information
export interface UserInfo {
// User Id
userId: string;
// Username
username: string;
// Avatar URL
profilePictureUrl: string;
// Game Id
gameId: number;
}
The username of JOGOS_SDK
is 6-20 characters, which can include letters, numbers, periods, and underscores.
The UserId of JOGOS_SDK corresponds one-to-one with the player's account; if your game allows creating multiple game characters or switching characters across multiple servers, your server needs to handle this logic to prevent players from losing multiple characters or being unable to switch.
Get User Token (Requires integration with your game server)
The user token contains information about the currently logged-in player. You should send it to your server when needed and verify/decode it there to extract relevant information, which is very useful for associating user accounts.
You can use the following method to retrieve the user token:
try {
const token = await window.JOGOS_SDK.user.getUserToken();
console.log('Get token result', token);
} catch (err) {
// Handle exceptions
console.error(err);
}
The token's lifespan is 1 hour. We recommend that you do not store the Token and always call this method when you need the Token.
If the current player is not registered or logged in, the system will automatically open the login/registration dialog box, requiring the user to register or log in successfully to continue the game; otherwise, it will return a 401 Unauthorized, please log in again
exception.
If the current player's account has been frozen or canceled, the corresponding exception information will also be returned.
The returned token can be decoded and tested on jwt.io.
The successfully returned token information includes the following data:
{
"jti": "1899405772252258304",
"sub": "{\"profilePictureUrl\":\"a33bf366e34d48e984a094fc923d7b06.png\",\"gameId\":\"1\",\"profileDefaultAvatar\":\"default-avatar.png\",\"userId\":\"88E1B72FED537C6943C85C28F639BE0B\",\"username\":\"55895999999999999999\"}",
"iss": "com:jogos:sdk",
"iat": 1741688600,
"exp": 1745288600
}
When you need to use the server to authenticate requests, you should send the token to your server, then decode and verify the token. You can use the public key hosted at this User Token Public Key
to verify the token. We recommend that you obtain the key each time you verify the token, as it may change.
The following is an example of decoding and verifying a token using TypeScript
and the public key:
import * as jwt from 'jsonwebtoken';
export interface CrazyTokenPayload {
userId: string;
username: string;
gameId: string;
profilePictureUrl: string;
}
export const decodeUserToken = async (token: string): Promise<CrazyTokenPayload> => {
const publickKey = `...`; // Obtained from https://www.jogos.com/sdk/public-key.txt
const payload = jwt.verify(token, key, { algorithms: ['RS256'] });
return payload as CrazyTokenPayload;
};
Local Testing
During local testing, the relevant interfaces of the user module only return simulated data.