Chat Flutter v3
Chat Flutter
Chat
Flutter
Home
/
Chat
/
Flutter

Authentication

In order to use the features of the Chat SDK in your client apps, a SendbirdSdk 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 Chat SDK's features. This page explains how to authenticate your user with the server.


Initialize with APP_ID

To use our chat features, you must initialize a SendbirdSdk instance by passing the APP_ID of your Sendbird application as an argument to a parameter of the SendbirdSdk constructor method. The initWithApplicationId: must be called once across your client app. Generally, initialization is implemented in the main.dart file.

// Initialize SendbirdSdk instance to use APIs in your app.
final sendbird = SendbirdSdk(appId: APP_ID);

Connect to the Sendbird server with a user ID

By default, the Sendbird server can authenticate a user with just a unique user ID. Then, the server queries the database to check for a match upon connection request. If no matching user ID is found, the server creates a new user account with the user ID. The ID should be unique within a Sendbird application to be distinguishable from other identifiers such as a hashed email address or a phone number in your service.

While authenticating with just the user ID is convenient in the developing and testing stages of a service, a more secure authentication process using tokens is strongly recommended for most production environments.

Note: Go to the event handler page to learn more about the usages of the Chat SDK's handlers and callbacks.

// The USER_ID below should be unique to your Sendbird application.
try {
    final user = await sendbird.connect(userId: USER_ID);
    // The user is connected to the Sendbird server.
} catch (e) {
    // Handle error.
}

Note: You must connect to the Sendbird server before calling any methods through the Chat SDK, apart from initializing a SendbirdSdk instance. If you attempt to call a method without connecting, a ConnectionRequiredError will be returned.


Connect to the Sendbird server with a user ID and an access token

Using Chat Platform API, you can create a user along with their own access token or issue an access token for an existing user. Once an access token is issued, a user is required to provide the access token when logging in to the Sendbird application.

  1. Using the Chat Platform API, create a Sendbird user account with information submitted when a user signs up or logs in to your service.

  2. Save the user ID along with the issued access token to your persistent storage which is securely managed.

  3. When the user attempts to log in to the application, load the user ID and access token from the storage, and then pass them to the connect() method.

  4. Periodically replacing the user's access token is recommended to protect the account.

Note: From Settings > Application > Security > Access token permission setting on your dashboard, you're able to prevent users without an access token from logging in to your Sendbird application or restrict their access to read and write messages.

// The USER_ID below should be unique to your Sendbird application.
try {
    final user = await sendbird.connect(userId: USER_ID, accessToken: AUTH_TOKEN);
    // The user is authenticated using the access token and is connected to the Sendbird server.
} catch (e) {
    // Handle error.
}

Note: For stronger security, you can use a session token instead of an access token when a user logs in to the Sendbird server. See the access token vs. session token section in the Platform API guide to learn more.


Disconnect from the Sendbird server

A user should be disconnected from the Sendbird server when they no longer need to receive messages from an online state. However, the user will still receive push notifications for new messages from group channels they've joined.

When disconnected, all event handlers in a user's client app registered by the addChannelEventHandler() or addConnectionEventHandler() methods stop receiving event callbacks from the server. Then, all internally cached data in the client app, such as the channels that are cached when the getChannel() method of the OpenChannel or GroupChannel is called, are also flushed.

Note: By default, most of the data related to users, channels, and messages are internally cached in the SendbirdSdk instance of a user's client app, which are retrieved by the corresponding query instances or received through the event handlers.

sendbird.disconnect();

Update user profile

Using the updateCurrentUserInfo() method, you can update a user's nickname and profile image with a URL.

try {
    await sendbird.updateCurrentUserInfo(
        nickname: NICKNAME,
        fileInfo: FileInfo.fromUrl(url: PROFILE_URL)
    );
    // The current user's profile is successfully updated.
    // You could redraw the profile in a view in response to this operation.
}

Or you can directly upload a profile image like the following code.

try {
    await sendbird.updateCurrentUserInfo(
        nickname: NICKNAME,
        fileInfo: FileInfo.fromData(file: PROFILE_DATA)
    );
    // The current user's profile is successfully updated.
    // You could redraw the profile in a view in response to this operation.
}

Note: A user's profile image can be a JPG (.jpg), JPEG (.jpeg), or PNG (.png) file of up to 25 MB.