getToken method

Future<Map<String, dynamic>> getToken({
  1. required String userId,
})

Implementation

Future<Map<String, dynamic>> getToken({required String userId}) async {
  final jwt = userJwt;
  final secret = appSecret;
  late Uri uri;
  final headers = <String, String>{
    'Content-Type': 'application/json',
    'X-App-Id': appId,
  };
  if (jwt != null && jwt.isNotEmpty) {
    uri = Uri.parse('$_base/api/user/im/token');
    headers['Authorization'] = 'Bearer $jwt';
  } else if (secret != null && secret.isNotEmpty) {
    uri = Uri.parse('$_base/api/server/im/token');
    headers['X-App-Secret'] = secret;
  } else {
    throw StateError('set userJwt or appSecret before getToken');
  }
  final res = await http.post(
    uri,
    headers: headers,
    body: jsonEncode({'appId': appId, 'userId': userId}),
  );
  return _decode(res);
}