onboard method

Future<bool> onboard({
  1. String? cramSecret,
  2. AtOnboardingRequest? atOnboardingRequest,
})

Returns true if authentication is successful for the existing atsign in device.

Implementation

Future<bool> onboard(
    {String? cramSecret, AtOnboardingRequest? atOnboardingRequest}) async {
  _atsign ??= await getAtSign();
  if (_atsign == null || _atsign!.isEmpty) {
    _logger.severe('atSign is not found');
    throw OnboardingStatus.ATSIGN_NOT_FOUND;
  }
  AtAuthService authService =
      AtClientMobile.authService(_atsign!, atClientPreference);
  bool isAtSignOnboarded = await authService.isOnboarded(_atsign!);
  // If atSign is onboarded, authenticate the atSign. Else onboard the atSign.
  if (isAtSignOnboarded) {
    AtAuthRequest atAuthRequest = AtAuthRequest(_atsign!);
    AtAuthResponse atAuthResponse =
        await authService.authenticate(atAuthRequest);
    return atAuthResponse.isSuccessful;
  }
  var onboardingResponse = await authService.onboard(
      AtOnboardingRequest(_atsign!)
        ..appName = (atOnboardingRequest != null &&
                atOnboardingRequest.appName != null)
            ? atOnboardingRequest.appName
            : 'system'
        ..deviceName = (atOnboardingRequest != null &&
                atOnboardingRequest.deviceName != null)
            ? atOnboardingRequest.deviceName
            : 'default-device',
      cramSecret: cramSecret);
  _logger.finer('onboardingResponse: $onboardingResponse');
  if (onboardingResponse.isSuccessful) {
    // Initializing "AtClientService" and adding to it map for backward compatibility.
    // Invoke init() method to initialize AtClient and AtLookup instances in atClientService.
    // ignore: deprecated_member_use, deprecated_export_use
    AtClientService atClientService = AtClientService();
    // ignore: deprecated_export_use
    // ignore: deprecated_member_use_from_same_package
    atClientServiceMap.putIfAbsent(_atsign, () => atClientService);
  }
  return onboardingResponse.isSuccessful;
}