Exemple #1
0
        public async Task <Unit> Handle(SignUpAnonymousCommand request, CancellationToken cancellationToken)
        {
            try
            {
                using var transaction = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled);

                string generatedPassword = _nameGenerator.GenerateAnonymousPassword();
                var    user = new User(UserType.Anonymous)
                {
                    UserName = _nameGenerator.GenerateAnonymousName()
                };

                var result = await _userManager.CreateAsync(user, generatedPassword);

                if (result.Succeeded)
                {
                    await _signInManager.SignInAsync(user, isPersistent : false);
                }
                else
                {
                    var errorMessages = result.Errors.Select(e => e.Description);
                    throw new InvalidOperationException(string.Join("\n", errorMessages));
                }

                transaction.Complete();

                return(Unit.Value);
            }
            catch (InformativeException exception)
            {
                _logger.LogError(exception, "Could not sign up as anonymous");
                throw;
            }
            catch (Exception exception)
            {
                _logger.LogError(exception, "Could not sign up as anonymous");
                throw new InformativeException("Could not sign up as anonymous. Please retry");
            }
        }