authenticate method

Future<User> authenticate({
  1. required String identityToken,
  2. String? challengeToken,
  3. bool? emailVerified,
  4. String? name,
  5. List<String>? tags,
  6. UserAuthenticationPolicy? policy,
  7. bool? doNotNotify,
})

Implementation

Future<User> authenticate({
  required String identityToken,
  String? challengeToken,
  bool? emailVerified,
  String? name,
  List<String>? tags,
  UserAuthenticationPolicy? policy,
  bool? doNotNotify,
}) async {
  if (challengeToken == null) {
    final result = await _auth.challenge();
    challengeToken = result.challengeToken;
  }

  final attestationHash = base64.encode(
    sha256.convert(utf8.encode("$identityToken:$challengeToken")).bytes,
  );
  final attest = await _attestation
      .attest({"hash": attestationHash})
      .catchError((error) {
        developer.log(
          "Failed to attest, this is fatal error unless it is in debug mode",
          name: "calljmp",
          error: error,
        );
        return Null;
      });
  final attestationToken = base64.encode(utf8.encode(jsonEncode(attest)));

  final result = await http
      .request("${_config.serviceUrl}/users/auth/provider/${_provider.value}")
      .use(http.context(_config))
      .post({
        "challengeToken": challengeToken,
        "attestationToken": attestationToken,
        "identityToken": identityToken,
        if (emailVerified != null) "emailVerified": emailVerified,
        if (name != null) "name": name,
        if (tags != null) "tags": tags,
        if (policy != null) "policy": policy.value,
        "doNotNotify": doNotNotify ?? false,
      })
      .json(
        (json) => (
          accessToken: json["accessToken"],
          user: User.fromJson(json["user"]),
        ),
      );

  await CalljmpStore.instance.put(
    CalljmpStoreKey.accessToken,
    result.accessToken,
  );

  return result.user;
}