loginWithAuthToken static method
- String authToken, {
- required dynamic onSuccess(
- User user
- required dynamic onError(
- CometChatException excep
Returns a User object after login in CometChat API.
The CometChat SDK maintains the session of the logged in user within the SDK. Thus you do not need to call the login method for every session. You can use the CometChat.getLoggedInUser() method to check if there is any existing session in the SDK. This method should return the details of the logged-in user.
Create an Auth Token via the CometChat API for the new user every time the user logs in to your app
check of getLoggedInUser() function in your app where you check App's user login status. In case it returns nil then you need to call the Login method inside it.
Migration Note: Migrated from platform channels to native Dart implementation. Behavior and signature remain identical for backward compatibility.
Android Reference: CometChat.login(String authToken, CallbackListener)
method could throw PlatformException with error codes specifying the cause
Implementation
static Future<User?> loginWithAuthToken(String authToken,
{required Function(User user)? onSuccess,
required Function(CometChatException excep)? onError}) async {
try {
// Get SDK instance
final sdk = SdkRegistry.getInstance();
// Call native Dart auth repository
final user = await sdk.auth.loginWithAuthToken(authToken);
// Notify login listeners
_loginListeners.values.forEach((element) {
element.loginSuccess(user);
});
// Call success callback
if (onSuccess != null) onSuccess(user);
return user;
} on SdkException catch (sdkEx) {
// Convert SdkException to CometChatException
final cometChatEx = CometChatException(
sdkEx.code,
sdkEx.details ?? sdkEx.message,
sdkEx.message,
);
// Notify login listeners
_loginListeners.values.forEach((element) {
element.loginFailure(cometChatEx);
});
_errorCallbackHandler(cometChatEx, null, null, onError);
} catch (e) {
// Handle unexpected errors
final cometChatEx = CometChatException(
ErrorCode.errorUnhandledException,
e.toString(),
e.toString(),
);
// Notify login listeners
_loginListeners.values.forEach((element) {
element.loginFailure(cometChatEx);
});
_errorCallbackHandler(null, null, e, onError);
}
return null;
}