In production, never include the authKey in your client-side code. The authKey grants full administrative access (logging in as any user, creating/updating users) and can be easily extracted from a decompiled APK or IPA.
The Secure Workflow
- Server-Side Token Generation: Your backend should generate a server-minted
authToken for the user. - Client Login: Use
CometChatUIKit.loginWithAuthToken() instead of CometChatUIKit.login(). - User Management: All user creation and profile updates must be performed server-side, not via the Flutter client.
Handling Token Expiry
If you have configured token expiry in the CometChat dashboard, handle the error in the onError callback to trigger a token refresh from your backend.
// ✅ SECURE: Use server-minted tokens in production
final settings = (UIKitSettingsBuilder()
..appId = appId
..region = region
..subscriptionType = CometChatSubscriptionType.allUsers)
.build();
// Login using the token
CometChatUIKit.loginWithAuthToken(authToken,
onSuccess: (user) {
// Proceed
},
onError: (e) {
if (e.code == 'ERR_AUTH_TOKEN_NOT_FOUND' || e.code == 'AUTH_ERR_AUTH_TOKEN_NOT_FOUND') {
// Token expired or invalid — fetch a new one from your backend
refreshAndRetryLogin();
}
},
);