authenticateWithUsernamePassword method
Authenticate to this CARP service using a username
and password
.
This method needs a BuildContext to authenticate but it does not open a web view. Use this if you want to create your own authentication page.
The discovery URL is used to find the Identity Server.
Return the signed in user (with an OAuthToken access token), if successful. Throws a CarpServiceException if not successful.
Implementation
Future<CarpUser> authenticateWithUsernamePassword({
required String username,
required String password,
}) async {
final TokenResponse? response = await appAuth.token(
TokenRequest(
app.clientId,
"${app.redirectURI}",
clientSecret: app.clientSecret ?? '',
discoveryUrl: "${app.discoveryURL}",
grantType: 'password',
additionalParameters: Map.fromEntries([
MapEntry('username', username),
MapEntry('password', password),
]),
),
);
if (response != null) {
currentUser.authenticated(OAuthToken.fromTokenResponse(response));
_authEventController.add(AuthEvent.refreshed);
return currentUser;
}
// All other cases are treated as a failed attempt and throws an error
_authEventController.add(AuthEvent.failed);
_currentUser = null;
// auth error response from CARP is on the form
// {error: invalid_grant, error_description: Bad credentials}
throw CarpServiceException(
httpStatus: HTTPStatus(401),
message: 'Authentication failed.',
);
}