refresh method
Get a new access token for the current user based on the previously granted refresh token, using the Identity Server discovery URL.
This method is typically used when the access token has expired, and a new access token is needed to access the CARP web service. The refresh token expiration date is OAuthToken.expiresAt which has type DateTime.
Returns the signed in user (with a new OAuthToken access token), if successful. Throws a CarpServiceException if not successful.
Implementation
Future<CarpUser> refresh() async {
final TokenResponse? response = await appAuth.token(
TokenRequest(
app.clientId,
clientSecret: app.clientSecret ?? '',
"${app.redirectURI}",
discoveryUrl: "${app.discoveryURL}",
refreshToken: currentUser.token!.refreshToken,
),
);
if (response != null) {
currentUser = getCurrentUserProfile(response);
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.',
);
}