# User Identity

When users log in or log out of the app, make sure to synchronize the status with AIHelp to confirm or reset the user identity.

# API

# login()

When users log into the app, synchronize the user ID with AIHelp so that we can generate a identity token for them.

AIHelpSupport.login("THIS IS YOUR USER ID");

Alternatively, you can achieve other capabilities by configuring LoginConfig, such as other user information, enterprise-level identity authorization, etc.:

let userConfig = {
    userName: "AIHelper",
    userTags: "recharge,suggestion",
    customData: {
        key: 'value'
    }
};
let loginConfig = {
    userId: "THIS IS YOUR USER ID",
    userConfig: userConfig,
    isEnterpriseAuth: false
}
AIHelpSupport.login(loginConfig);

# logout

When users log out, call this method to clear the current logged-in user information to ensure the accuracy of the guest identity after logging out.

AIHelpSupport.logout();

# URL

Or, you could implement the same function by the URL scheme:

<script>
  let domain = "THIS IS YOUR APP DOMAIN";
  let appId = "THIS IS YOUR APP ID";
  let entranceId = "THIS IS YOUR ENTRANCE ID";
  (function () {
    let baseUrl = `https://${domain}/webchatv5/#/${appId}`;
    let params = `entranceId=${entranceId}&userId=${your_user_id}&userName=${your_user_name}&userTags=${your_user_tags}&customData=${encodeURIComponent(JSON.stringify({key:"value"}))}`;
    let url = `${baseUrl}?${params}`;
    window.open(url);
  })();
</script>

# Event

# Logging in

You can register the userLogin event to keep track of the logging in status for current user:

AIHelpSupport.registerAsyncEventListener("userLogin", function(jsonData) {
    // `jsonData`: { "code": 1, "message": "Success" }
});

The code field for jsonData can be one of:

  • 1 for success;
  • -1 for invalid user id;
  • -2 for failure, check the message field for more information;

# Authentication

If you intend to authenticate user by your side by configuring isEnterpriseAuth: true, the enterpriseAuth event will be triggered when login API is invoked.

And you should register the event and authenticate the user, and then pass the retrieved user token back to us via the ack callback.

AIHelpSupport.registerAsyncEventListener("enterpriseAuth", function(jsonData, ack) {
    ack("{'token':'your async token'}")
});

For compatibility purposes, the data passed back to AIHelp should be in JSON format. The user token should be stored under the key token.

# Definition

# loginConfig

  • Type: object
  • Details: Required. Encapsulation of optional configuration items for login.

# userId

  • Type: String
  • Default: A random number generated by the user's device
  • Details: Required. Unique user identifier, cannot be an empty string, 0, or -1.

# userConfig

  • Type: UserConfig
  • Default: null
  • Details: Optional. User information configuration items, including username, server ID, user tags, and other custom data. Besides logging in, you can also call updateUserInfo API to update user information whenever you need.
  • Code example:
    let userConfig = {
        userName: "AIHelper",
        userTags: "recharge,suggestion",
        customData: {
            key: 'value'
        }
    };
    let loginConfig = {
        userId: "THIS IS YOUR USER ID",
        userConfig: userConfig
    }
    AIHelpSupport.login(loginConfig);
    

# isEnterpriseAuth

  • Type: boolean
  • Default: false
  • Details: Optional. You need to register the enterpriseAuth event to obtain the user token if you enable this field.
  • Code example:
      let loginConfig = {
          userId: "THIS IS YOUR USER ID",
          isEnterpriseAuth: true
      }
      AIHelpSupport.login(loginConfig);
    

# Other

# Enterprise Authorization Process

To maximize security, you can opt to handle user authentication logic yourself, known as enterprise authorization.

Enterprise authorization is a type of server-side validation, where the validation process is completely managed on the server, and the SDK and game client do not hold any keys.

The enterprise authentication logic sequence is as follows:

  1. The game client calls the SDK login interface, specifying the userId and enabling enterprise authentication;
  2. The SDK sends an enterprise authentication event. After the client receives the corresponding callback, it requests the game server to authenticate the player;
  3. After receiving the server's response, the game client uses the ack callback in the enterprise authentication event to return the token issued by the server to the SDK;
  4. The SDK uses the received token to request the AIHelp server interface. The server will perform a secondary validation of the token; if the token is not valid, a login failure will be returned.

# Token Generation Rules

During the enterprise authentication process, you need to obtain the secret key from the AIHelp backend and issue a token for the user on the server.

The token generation rules are as follows:

  1. Use the secret key issued by AIHelp, which can be obtained from the backend under Settings - Application Settings - SecretKey;
  2. Use the HS256 signing algorithm, with a recommended validity period of 24 hours;
  3. Generate a JWT in the standard format, as follows:
{
    "exp": 1718868267, // Expiration timestamp (seconds)
    "iat": 1718781867, // Issuance timestamp (seconds)
    "nbf": 1718781867, // Not Before timestamp (seconds)
    "iss": "xxxx", // Custom identifier
    "jti": "947b3086-9aad-40d8-b5cf-752c9294f392" // UUID
}
Last Updated: 7/26/2024, 10:14:16 AM