private async Task <Result <EnrollDeviceCommandResult, ErrorData> > Process(EnrollDeviceCommand request,
                                                                                    CancellationToken cancellationToken)
        {
            var whenHappened     = this._clock.GetCurrentInstant().ToDateTimeUtc();
            var currentUserMaybe = this._currentUserService.CurrentUser;

            if (currentUserMaybe.HasNoValue)
            {
                return(Result.Fail <EnrollDeviceCommandResult, ErrorData>(new ErrorData(ErrorCodes.UserNotFound)));
            }

            var userMaybe =
                await this._userRepository.Find(this._currentUserService.CurrentUser.Value.UserId, cancellationToken);

            if (userMaybe.HasNoValue)
            {
                return(Result.Fail <EnrollDeviceCommandResult, ErrorData>(new ErrorData(ErrorCodes.UserNotFound)));
            }

            var user = userMaybe.Value;

            Fido2.CredentialMakeResult credentialMakeResult;
            try
            {
                // 2. Verify and make the credentials
                Task <bool> IsCredentialIdUniqueToUser(IsCredentialIdUniqueToUserParams param)
                {
                    return(Task.FromResult(user.AuthenticatorDevices.Count == 0));
                }

                credentialMakeResult = await this._fido2.MakeNewCredentialAsync(
                    request.AuthenticatorAttestationRawResponse,
                    request.CredentialCreateOptions,
                    IsCredentialIdUniqueToUser);
            }
            catch (Fido2VerificationException e)
            {
                return(Result.Fail <EnrollDeviceCommandResult, ErrorData>(
                           new ErrorData(ErrorCodes.FidoVerifcationFailed)));
            }

            user.EnrollAuthenticatorDevice(Guid.NewGuid(),
                                           whenHappened,
                                           credentialMakeResult.Result.PublicKey,
                                           credentialMakeResult.Result.CredentialId,
                                           credentialMakeResult.Result.Aaguid,
                                           Convert.ToInt32(credentialMakeResult.Result.Counter),
                                           request.Name,
                                           credentialMakeResult.Result.CredType
                                           );

            return(Result.Ok <EnrollDeviceCommandResult, ErrorData>(new EnrollDeviceCommandResult(credentialMakeResult)));
        }
        public async Task <Result <EnrollDeviceCommandResult, ErrorData> > Handle(EnrollDeviceCommand request,
                                                                                  CancellationToken cancellationToken)
        {
            var result = await this.Process(request, cancellationToken);

            var dbResult = await this._userRepository.UnitOfWork.SaveEntitiesAsync(cancellationToken);

            if (!dbResult)
            {
                return(Result.Fail <EnrollDeviceCommandResult, ErrorData>(new ErrorData(
                                                                              ErrorCodes.SavingChanges, "Failed To Save Database")));
            }

            return(result);
        }