authenticate method
Performs an authentication attempt to the server side. Useful for the login page or to authenticate with tokens automatically in the startup of your App.
Important: authenticate(..)
will be called automatically by using the same credential
when
the user loses the internet connection and connects again,
but if it fails onAutoReauthenticationFails(...)
will be triggered
If AuthenticateResponse.success is true: the current user will be able to interact with routes
on the server side created with addRouteFor.authenticatedUsers
credential
Customized data you will use in the backend side to validate the authentication
request
neverTimeout
Default: false
(optional). If true
: the request attempt will live as long as possible.
If false
: if the request doesn't receive a response within the time limit, it will be canceled.
The field requestTimeoutInMs
defined on the server side will be the time limit.
Example
final authenticateResponse = await AsklessClient.instance.authenticate(credential: { "accessToken": accessToken });
if (authenticateResponse.success) {
log("user has been authenticated successfully");
} else {
log("connectWithAccessToken error: ${authenticateResponse.errorCode}");
if (authenticateResponse.isCredentialError) {
log("got an error: access token is invalid");
} else {
log("got an error with code ${authenticateResponse.errorCode}: ${authenticateResponse.errorDescription}");
}
}
Implementation
Future<AuthenticateResponse> authenticate ({required Map<String, dynamic> credential, bool neverTimeout = false}) async {
await getIt.get<AuthenticateService>().clearAuthentication();
return getIt.get<AuthenticateService>().authenticate(credential, neverTimeout: neverTimeout);
}