Chat UIKit SwiftUI v3
Chat UIKit SwiftUI
Chat UIKit
SwiftUI
Version 3

Authentication

Copy link

In order to use the features of Sendbird Chat SwiftUI in your client apps, a SendbirdUI instance must be initiated in each client app through user authentication with the Sendbird server. The instance communicates and interacts with the server using an authenticated user account, and is allowed to use the SwiftUI's features. This page explains how to authenticate your user with the server.

Prerequisites

Copy link
  • Sendbird SwiftUI should be initialized, for the details please refer Getting Started.

Overview

Copy link

Authentication is required to use Sendbird’s features and there are three methods:

  1. Guest Login: A token is not required. However, this method is not recommended for production due to security vulnerabilities. You use a UserID only to authenticate.
  2. Access Token: A token that does not expire. It can be generated by Platform API or from the Dashboard.
  3. Session Token: Highly recommended for enhanced security.

    Note: This document does not cover session handling. Please refer to this SDK document for more details.


In Sendbird SwiftUI, the basic way to authenticate is as follows:

  1. Set and store a UserID and token.
  2. When accessing a screen, Sendbird SwiftUI communicates with the Sendbird server using those credentials. This means Sendbird SwiftUI handles authentication internally and automatically.

How You Can Set the User ID and Token

Copy link
SBUGlobals.currentUser = SBUUser(userId: {USER_ID})
SBUGlobals.accessToken = {ACCESS_TOKEN}

Manual Connection

Copy link

You can manually connect to the Sendbird server and retrieve user information without accessing Sendbird SwiftUI’s screens. This is commonly required when you need to call SDK functions outside of Sendbird SwiftUI’s screens.

Example Use Case

Copy link

One example is registering a push token when a user logs into your service. To ensure notifications from Sendbird are not missed, you need to register the push token at the time of service login.

SendbirdUI.connect { (user, error) in
    guard let user = user else {
        // The user is offline and you can't access any user information stored in the local cache.
        return
    }
    if let error = error {
        // The user is offline but you can access user information stored in the local cache.
    }
    else {
        // The user is online and connected to the server.
    }
}

Logout

Copy link

Call the SendbirdUI.disconnect() method if the user requests to log out. If a user has been logged out and thus disconnected from the server, they will no longer receive messages.

NOTE: You must call unregisterPushToken to ensure the user does not receive notifications anymore.

// Highly recommended to call `unregisterPushToken` before disconnecting.
SendbirdUI.disconnect {}