User Setup & Tokens

Last Edit: Aug 08 2020

Tokens are used to authenticate the user. Typically, you send this token from your backend to the client when a user registers or logs in. You generate tokens on your server.

Basic Setup

You can quickly setup the user with a Token that you already have from our Dashboard and quickly test your chat without thinking about getting a user token from your backend.

let user = User(id: "john-doe")
Client.shared.set(user: user, token: "<#Token#>")

The Token Provider method will call your token service to get a token and finish user setup. You need to setup a callback with a tokenProvider function that you need to call when you get the token.

// Logged in user.
let user = User(id: "john-doe")

// Setup user for StreamChat Client.
Client.shared.set(user: user) { provide in 
    // Make a request here to your backend to generate a valid token for the user.
    YourTokenService.shared.getToken(for: user) { token

Expiring Tokens

By default, user tokens are valid indefinitely. Use Token Provider method to setup a user with an expiring token. Token Provider will call your token service automatically to refresh it when the user token is expired and resume all queued requests with refreshed token.

Development Token

For development applications, it is possible to disable token authentication and use client-side generated tokens.

⚠️ Disabling auth checks is not suitable for a production application and should only be done for proofs-of-concept and applications in the early development stage.

To enable development tokens, you need to change your application configuration in our Dashboard.

let user = User(id: "john-doe")
Client.shared.set(user: user, token: .development)

Guest Users

Guest sessions can be created client-side and do not require any server-side authentication.

Guest users have a limited set of permissions. You can read more about how to configure permissions here.

let user = User(id: "john-doe")
Client.shared.setGuestUser(user: user)

Anonymous Users

If a user is not logged in, you can call the setAnonymousUser method. While you’re anonymous, you can’t do much, but for the livestream channel type, you’re still allowed to read the chat conversation.


Creating Users

⚠️ This is not recommended to be used in client, let alone production. ⚠️

You might want to have a quick way of creating user tokens during development, and may not want to use stream js backend components or stream-cli. In that case, you can use this code snippet to create user tokens.

In your Podfile:

pod 'SwiftJWT'

wherever you'd like to generate tokens:

import SwiftJWT
private func generateUserToken(for userId: String, from apiSecret: String) throws -> String {
    let header = Header()
    let claims = StreamClaims(user_id: userId)
    var jwt = JWT(header: header, claims: claims)
    guard let secretData = .utf8) else {
        throw NSError(domain: "jwtTokenError", code: -1, userInfo: nil)
    let jwtSigner = JWTSigner.hs256(key: secretData)
    return try jwt.sign(using: jwtSigner).string

Then you can use Client.shared.set(user:token:) function, and your user will be generated automatically on stream backend. The User you're passing to set(user:token) should have the same ID as the generated JWT token.