login method
Handle a login of an existing user.
The refresh token in the returned
response can be used to extend a session.
Throws UnverifiedEmailException
if email is
not verified. Throws InvalidCredentialsException
if there is an incorrect username or password.
Implementation
Future<LoginSuccessResponse> login(String userName, String password) async {
assert(_isValidPassword(password),
'The password must be at least 8 characters long. It should contain at least one uppercase, one lowercase character and a number.');
final uri = _urlBase.getPath(_loginEndpoint);
final body = await JsonIsolate().encodeJson({
'user_name': userName,
'password': password,
});
final resp = await post(uri, body: body);
final Map<String, dynamic> bodyResp =
await JsonIsolate().decodeJson(resp.body);
if (resp.statusCode != 200) {
if (bodyResp['error_code'] == 101015) {
throw UnverifiedEmailException();
} else if (bodyResp['error_code'] == 101009) {
throw InvalidCredentialsException();
}
throw bodyResp['description'];
}
return LoginSuccessResponse.fromJson(bodyResp);
}