logo
On this page

Using Kit Token for authentication

Introduction

Kit Token is a credential the ZEGOCLOUD UIKit uses for authentication: to validate the user’s permission. It will also determine:

  • Which room the user will join
  • The unique identifier of the user in the room (userID)
  • Default username

ZEGOCLOUD UIKits provide the method to generate the Kit Token on the client app, while it's not safe enough for you to make your app go live officially. Here, we recommend you generate the Kit Token on your app server. Check this guide out:

Prerequisites

Step 1 Generate a Token

Warning
  • For your convenience during development and debugging, we provide the ZEGO Token Assistant to generate temporary Tokens.
  • For business security, you must generate Tokens on your app server; Otherwise, there is a risk of ServerSecret being stolen.
  • After generating a Token on your own server, if you need to verify its validity, you can also use the ZEGO Token Assistant for verification.

ZEGO Token Assistant

This tool runs locally in browser and does not send data to server. Do not expose ServerSecret in production, use for debugging only.

After getting your AppID and ServerSecret, you can define the validation rules on your app server or client based on your business requirements.

We provide an open-source Token generator plug-in on GitHub, which you can use to generate Tokens using different programming languages such as Go, C++, Java, Python, PHP,.NET, and Node.js.

LanguageSupported versionCore functionCode baseSample code
GoGo 1.14.15 or laterGenerateToken04
C++C++ 11  or laterGenerateToken04
JavaJava 1.8  or latergenerateToken04
PythonPython 3.6.8  or latergenerate_token04
PHPPHP 5.6 or latergenerateToken04
.NET.NET Framework 3.5  or laterGenerateToken04
Node.jsNode.js 8  or latergenerateToken04

Generate a Token (PHP as an example)

Here we recommend the Composer Autoload, that is the Composer's PSR-4 autoload.

1 Install the plug-in

a. Copy the downloaded package to the project's root directory. Let's take the /my_project/zego directory as an example, where /my_project/ is the root directory.

b. Include the psr-4 autoload config to the vim /my_project/composer.json file.

{
  ...
  "autoload": {
    "psr-4": {
      "ZEGO\\": "zego/src/ZEGO"
    }
  }
  ...
}

c. Run the composer dump-autoload, or composer dump-autoload -o(for production environment), or composer update command to generate an autoload file.

2. Generate the Token
  • Use the following in the /my_project/xxx.php file.
  • Leave the payload field empty (because the Token is only used for a simple permission validation for service API).
require 'vendor/autoload.php';
use ZEGO\ZegoServerAssistant;
use ZEGO\ZegoErrorCodes;
$appId = 1111;
$userId = 'demo';
$secret = 'You serverSecret';
$payload = '';
$token = ZegoServerAssistant::generateToken04($appId,$userId,$secret,3600,$payload);
if( $token->code == ZegoErrorCodes::success ){
   print_r(json_encode($token));
}

Step 2 Generate a Kit Token

To generate the Kit Token:

  • Fill in the token field in the following code with the Token you just generated in the previous steps
  • Fill in other fields and run the following code.
//... your own logic code
fetch(
      `${youServerUrl}?userID=${userID}&expired_ts=86400`,
      {
        method: "GET",
      }
    )
.then((res) => res.json())
.then(({token})=>{
  const kitToken = ZegoUIKitPrebuilt.generateKitTokenForProduction(
     appID,
     token,
     roomID,
     userID,
     userName
  );
 const zp = ZegoUIKitPrebuilt.create(kitToken);
 //... to joinRoom
})

Previous

In-room messages

On this page

Back to top