completeAuthentication method

  1. @override
Future<AuthenticationResult> completeAuthentication(
  1. AuthenticationProcess process,
  2. String validationCode,
  3. Tuple2<String, String> userKeyPair,
  4. Future<Tuple3<String, String, String>?> tokenAndKeyPairProvider(
    1. String,
    2. String
    ),
)
override

Completes the authentication process of a user, by verifying the provided validation code and :

  • In the case of a sign-up, create the user data;
  • In the case of a login, re-generate keys if needed (new keys different from previous ones);

Parameters

  • process The AuthenticationProcess previously provided in the startAuthentication service
  • validationCode The validation code the user received by email/mobile phone
  • userKeyPair The key pair private, public that will be used by the user to encrypt/decrypt data;
  • tokenAndKeyPairProvider A custom function to generate an authentication token and a key pair for user

Returns

  • The result of the authentication and the related MedTechApi object corresponding to the newly authenticated user.

Implementation

@override
Future<AuthenticationResult> completeAuthentication(
  AuthenticationProcess process,
  String validationCode,
  Tuple2<String, String> userKeyPair,
  Future<Tuple3<String, String, String>?> Function(String, String) tokenAndKeyPairProvider
) async {
  final Response res = await _validateAuthenticationProcess(process.requestId, validationCode, process.bypassTokenCheck);

  if (res.statusCode < 400) {
    final Tuple2<MedTechApi, ApiInitialisationResult> initInfo =
        await retry(() async => await _initApiAndUserAuthenticationToken(process, validationCode, tokenAndKeyPairProvider), trials: 5, delay: 1000);

    MedTechApi authenticatedApi =
        await _initUserCrypto(initInfo.item1, initInfo.item2.token, initInfo.item2.user, initInfo.item2.keyPair ?? userKeyPair);

    return AuthenticationResult(
        authenticatedApi, initInfo.item2.keyPair ?? userKeyPair, initInfo.item2.token, initInfo.item2.user.groupId!, initInfo.item2.user.id);
  }

  throw FormatException("iCure could not complete authentication process with requestId ${process.requestId}. Try again later.");
}