generateToken static method

  1. @Deprecated('Use generateCallToken(sessionId, onSuccess:, onError:) instead. ' 'Auth token is now managed internally after login().')
void generateToken(
  1. String sessionId,
  2. String userAuthToken, {
  3. required dynamic onSuccess(
    1. GenerateToken
    ),
  4. required dynamic onError(
    1. CometChatCallsException
    ),
})

Generates a call token for the given session ID using the provided userAuthToken.

@deprecated Use generateCallToken instead, which manages the auth token internally after login/loginWithAuthToken.

This method is retained for backward compatibility with v4 consumers. The userAuthToken is passed directly to the API call. The onSuccess callback receives a GenerateToken instance.

CometChatCalls.generateToken(sessionId, userAuthToken,
  onSuccess: (GenerateToken token) { ... },
  onError: (e) { ... },
);

Implementation

@Deprecated('Use generateCallToken(sessionId, onSuccess:, onError:) instead. '
    'Auth token is now managed internally after login().')
// ignore: deprecated_member_use_from_same_package
static void generateToken(String sessionId, String userAuthToken,
    {required Function(GenerateToken) onSuccess,
    required Function(CometChatCallsException) onError}) async {
  try {
    if (!isInitialized) {
      onError(CometChatCallsException.sdkNotInitialized());
      return;
    }

    final sessionIdError = InputValidator.validateSessionId(sessionId);
    if (sessionIdError != null) {
      onError(sessionIdError);
      return;
    }

    // v4 path: use the passed-in auth token directly, matching Android's
    // deprecated generateToken(sessionId, authToken, listener).
    // Fall back to internal auth if the passed token is empty.
    String? authToken =
        userAuthToken.isNotEmpty ? userAuthToken : await getUserAuthToken();
    if (authToken == null || authToken.isEmpty) {
      onError(CometChatCallsException.userAuthTokenNull());
      return;
    }

    // Ensure the v5 SDK is logged in so getUserAuthToken() and other
    // internal operations work for subsequent calls. If the user is
    // already logged in with this token, loginWithAuthToken returns
    // immediately without a network call.
    _ensureLoggedIn(authToken, () {
      final CallToken callToken = CallToken();
      callToken.sessionID = sessionId;
      ApiConnection().generateToken(
          callToken: callToken,
          authToken: authToken,
          onSuccess: (success) {
            try {
              final Map<String, dynamic> result = jsonDecode(success);
              final CallToken token =
                  CallToken.fromJson(result[ResponseKeyConstants.data]);
              // ignore: deprecated_member_use_from_same_package
              onSuccess(GenerateToken.fromCallToken(token));
            } catch (e) {
              onError(CometChatCallsException.jsonParseError(e.toString()));
            }
          },
          onError: onError);
    }, onError);
  } catch (e) {
    onError(CometChatCallsException.wrapUnhandled(e));
  }
}