startAuthentication method
Future<AuthenticationProcess>
startAuthentication(
- String healthcareProfessionalId,
- String firstName,
- String lastName,
- String recaptcha,
- bool bypassTokenCheck, {
- String? email,
- 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 userfirstName
The firstname of the user to authenticatelastName
The lastname of the user to authenticaterecaptcha
The recaptcha key used during authentication processbypassTokenCheck
Prevent the token check during the validation processemail
The email of the user to authenticatemobilePhone
The mobile phone of the user to authenticate
Returns
- The AuthenticationProcess information needed to complete the authentication in the completeAuthentication service
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");
}