startAuthentication method

  1. @override
Future<AuthenticationProcess> startAuthentication(
  1. String healthcareProfessionalId,
  2. String firstName,
  3. String lastName,
  4. String recaptcha,
  5. bool bypassTokenCheck, {
  6. String? email,
  7. String? mobilePhone,
})
override

Starts the authentication of a user by sending him/her a validation code by email and/or mobile phone. Use this service if you would like to sign-up or login your user

Parameters:

  • healthcareProfessionalId The id of the healthcare professional that wants to invite the user for its authentication. Use the id of the hcp in charge of the database where you want to add this new user
  • firstName The firstname of the user to authenticate
  • lastName The lastname of the user to authenticate
  • recaptcha The recaptcha key used during authentication process
  • bypassTokenCheck Prevent the token check during the validation process
  • email The email of the user to authenticate
  • mobilePhone The mobile phone of the user to authenticate

Returns

Implementation

@override
Future<AuthenticationProcess> startAuthentication(
  String healthcareProfessionalId,
  String firstName,
  String lastName,
  String recaptcha,
  bool bypassTokenCheck,
  {String? email, String? mobilePhone}
) async {

  if (email == null && mobilePhone == null) {
    throw FormatException("In order to start authentication of a user, you should at least provide its email OR its mobilePhone");
  }

  final requestId = Uuid().v4(options: {'rng': UuidUtil.cryptoRNG});
  final date = DateTime.now().millisecondsSinceEpoch ~/ 60000;
  final String recaptchaHash = sha256.convert((date.toString() + ':' + recaptcha).codeUnits).toString();

  var client = Client();
  final Response res = await client.post(Uri.parse('${authServerUrl}/process/${authProcessId}/${requestId}'),
      headers: {'Content-Type': 'application/json'},
      body: await serializeAsync({
        'g-recaptcha-response': recaptchaHash,
        'firstName': firstName,
        'lastName': lastName,
        'from': email ?? mobilePhone,
        'mobilePhone': mobilePhone,
        'email': email,
        'hcpId': healthcareProfessionalId
      }));

  if (res.statusCode < 400) {
    return AuthenticationProcess(requestId, (email ?? mobilePhone)!, bypassTokenCheck);
  }

  throw Exception("iCure could not start the authentication process $authProcessId for user ${email ?? mobilePhone}. Try again later");
}