authenticate method
Implementation
Future<AuthenticateResponse> authenticate(credential, {bool neverTimeout = false, bool ignoreConnectionStatus = false}) async {
if (_authStatus == AuthStatus.inProgress) {
return AuthenticateResponse<USER_ID>(
null,
null,
AsklessAuthenticateError(
code: AsklessErrorCode.conflict,
isCredentialError: false,
description: "There's already a authentication in progress"
)
);
}
_authStatus = AuthStatus.inProgress;
if (!ignoreConnectionStatus) {
final connected = await getIt
.get<ConnectionService>()
.waitForConnectionOrTimeout(timeout: neverTimeout ? null : const Duration(seconds: 3));
if (!connected) {
return AuthenticateResponse(
null,
null,
AsklessAuthenticateError(
code: AsklessErrorCode.noConnection,
isCredentialError: false,
description: "No connection"
)
);
}
}
final res = AuthenticateResponseCli.fromResponse(await getIt
.get<RequestsService>()
.runOperationInServer(
data: AuthenticateRequestCli(
clientIdInternalApp: ConnectionService.clientIdInternalApp,
credential: credential
),
neverTimeout: neverTimeout,
isPersevere: () => false,
)
);
if (res.success && res.userId != null) { // Checking the res.userId in case the user is switching back to not authenticated, like in the catalog example.
_shouldBeAuthenticated = true;
_credential = credential;
_userId = res.userId;
_claims = res.claims;
_authStatus = AuthStatus.authenticated;
_completeAuthentication();
} else {
_authStatus = AuthStatus.notAuthenticated;
}
return AuthenticateResponse(
res.userId,
res.claims,
res.success
? null
: AsklessAuthenticateError(
isCredentialError: res.isCredentialError,
code: res.error!.code,
description: res.error!.description
));
}