Custom Token Authentication
Authenticate using a server generated JWT token
With Firebase, you can also authenticate using a custom token that's been generated on your server. It works like this:
  1. 1.
    The user authenticates with your server
  2. 2.
    The server generates a new authentication token based on the user's unique ID
  3. 3.
    The token is passed back to the client and into the Chat SDK
Generating the token
To generate a token, you should follow the Firebase custom authentication guide.
Firebase also has an Admin SDK for Node.js, Java, Python and Go which makes the process more straightforward. you can install it using this guide.
In PHP, an implementation may look like this:
1
// Get your service account's email address and private key from the JSON key file
2
$service_account_email = "[email protected]";
3
$private_key = "-----BEGIN PRIVATE KEY-----...";
4
​
5
function create_custom_token($uid, $is_premium_account) {
6
global $service_account_email, $private_key;
7
​
8
$now_seconds = time();
9
$payload = array(
10
"iss" => $service_account_email,
11
"sub" => $service_account_email,
12
"aud" => "https://identitytoolkit.googleapis.com/google.identity.identitytoolkit.v1.IdentityToolkit",
13
"iat" => $now_seconds,
14
"exp" => $now_seconds+(60*60), // Maximum expiration time is one hour
15
"uid" => $uid,
16
"claims" => array(
17
"premium_account" => $is_premium_account
18
)
19
);
20
return JWT::encode($payload, $private_key, "RS256");
21
}
Copied!
The id should be the id your server uses to identify the user who is currently logged in. This token should be passed back to the app.
Authenticating on the client
Java
Objective-C
1
AccountDetails details = new AccountDetails.token("Your token");
2
ChatSDK.auth().authenticate(details).subscribe(...);
Copied!
1
BAccountDetails * details = [BAccountDetails token:@"Your token"];
2
[BChatSDK.auth authenticate: details].thenOnMain(...);
Copied!
Copy link