AI Integration Quick Reference
AI Integration Quick Reference
login() method.
How It Works
Before You Log In
Create a User
A user must exist in CometChat before they can log in.- During development: Create users from the CometChat Dashboard. Five test users are already available with UIDs
cometchat-uid-1throughcometchat-uid-5. - In production: Call the Create User REST API when a user signs up in your app.
We have setup 5 users for testing having UIDs:
cometchat-uid-1, cometchat-uid-2, cometchat-uid-3, cometchat-uid-4 and cometchat-uid-5.Check for an Existing Session
The SDK persists the logged-in user’s session locally. Before callinglogin(), always check whether a session already exists — this avoids unnecessary login calls and keeps your app responsive.
getLoggedInUser() returns null, no active session exists and you need to call login().
Login with Auth Key
This straightforward authentication method is ideal for proof-of-concept (POC) development or during the early stages of application development. For production environments, however, we strongly recommend using an Auth Token instead of an Auth Key to ensure enhanced security.- Dart
On success, the
onSuccess callback receives a User object containing the logged-in user’s details.
Response
Response
On Success — A
User object representing the logged-in user:User Object:Error
Error
CometChatException:
Login with Auth Token
Auth Token login keeps your Auth Key off the client entirely. Your server generates a token via the REST API and passes it to the client.- Create the user via the REST API when they sign up (first time only).
- Generate an Auth Token on your server and return it to the client.
- Pass the token to
loginWithAuthToken().
- Dart
On success, the
onSuccess callback receives a User object containing the logged-in user’s details.
Response
Response
On Success — A
User object representing the logged-in user:User Object:Error
Error
CometChatException:
Logout
Calllogout() when your user logs out of your app. This clears the local session.
- Dart
Response
Response
On Success — A
String message confirming the logout:Error
Error
CometChatException:
Login Listener
You can listen for login and logout events in real time usingLoginListener. This is useful for updating UI state or triggering side effects when the auth state changes.
Add a Listener
- Dart
Remove a Listener
Next Steps
Send Messages
Send your first text, media, or custom message
User Management
Create, update, and manage users in your app
Connection Status
Monitor the SDK connection state in real time
All Real-Time Listeners
Complete reference for all SDK event listeners