# 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.

[AIHelpSupportSDK loginWithUserId:@"THIS IS YOUR USER ID"];

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



 
 


AIHelpLoginConfigBuilder *builder = [[AIHelpLoginConfigBuilder alloc] init];
builder.userId = @"THIS IS YOUR USER ID";
builder.userConfig = ...;
builder.isEnterpriseAuth = ...;
[AIHelpSupportSDK login:builder.build];

# 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();

# Event

# Logging in

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

void loginListener(const char *jsonData, void (*ignored)(const char *data)) {
   // `jsonData`: { "code": 1, "message": "Success" } 
}
[AIHelpSupportSDK registerAsyncListener:loginListener eventType:AIHelpEventUserLogin];

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 setEnterpriseAuth, the AIHelpEventEnterpriseAuth 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.

void listener(const char *ignored, void (*ack)(const char *ackData)) {
    std::thread([ack]() {
        ack("{'token':'your async token'}");
    }).detach();
}
[AIHelpSupportSDK registerAsyncListener:listener eventType:AIHelpEventEnterpriseAuth];

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: AIHelpLoginConfig
  • Details: Required. Encapsulation of optional configuration items for login.

# userId

  • Type: NSString
  • 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: AIHelpUserConfig
  • 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:
     
     
     
     
     



     


    AIHelpUserConfigBuilder *userBuilder = [[AIHelpUserConfigBuilder alloc] init];
    userBuilder.userName = @"AIHelper";
    userBuilder.serverId = @"s-101";
    userBuilder.userTags = @"vip1,suggestion";
    userBuilder.customData = @{@"total_recharge":@10000, @"level":@34};
    
    AIHelpLoginConfigBuilder *builder = [[AIHelpLoginConfigBuilder alloc] init];
    builder.userId = @"THIS IS YOUR USER ID";
    builder.userConfig = userBuilder.build;
    [AIHelpSupportSDK login:builder.build];
    

# isEnterpriseAuth

  • Type: boolean
  • Default: false
  • Details: Optional. You need to register the AIHelpEventEnterpriseAuth event to obtain the user token if you enable this field.
  • Code example:


     


    AIHelpLoginConfigBuilder *builder = [[AIHelpLoginConfigBuilder alloc] init];
    builder.userId = @"THIS IS YOUR USER ID";
    builder.isEnterpriseAuth = YES;
    [AIHelpSupportSDK login:builder.build];
    

# 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