示例#1
0
        public async Task SignUpAsync(SignUp command)
        {
            if (!EmailRegex.IsMatch(command.Email))
            {
                _logger.LogWarning($"Invalid email address: {command.Email}.");
                throw new InvalidEmailException(command.Email);
            }

            var user = await _userRepository.GetAsync(command.Email);

            if (!(user is null))
            {
                var exception = new EmailInUseException(command.Email);
                await _messageBroker.PublishAsync(new SignUpRejected(command.Email, exception.Message, exception.Code));

                throw exception;
            }

            var role     = string.IsNullOrWhiteSpace(command.Role) ? "user" : command.Role.ToLowerInvariant();
            var password = _passwordService.Hash(command.Password);

            user = new User(command.UserId, command.Email, password, role, DateTime.UtcNow);
            await _userRepository.AddAsync(user);

            await _messageBroker.PublishAsync(new SignedUp(user.Id, user.Email, user.Role));

            _logger.LogInformation($"Created an account for the user with id: {user.Id}.");
        }
示例#2
0
        public async Task SignUpAsync(SignUp command)
        {
            if (!EmailRegex.IsMatch(command.Email))
            {
                throw new InvalidEmailException(command.Email);
            }

            var user = await _userRepository.GetAsync(command.Email);

            if (!(user is null))
            {
                var exception = new EmailInUseException(command.Email);
                await _messageBroker.PublishAsync(new SignUpRejected(command.Email, exception.Message, exception.Code));

                throw exception;
            }

            var role     = string.IsNullOrWhiteSpace(command.Role) ? "user" : command.Role.ToLowerInvariant();
            var password = _passwordService.Hash(command.Password);

            user = new User(command.Id, command.Email, password, role, DateTime.UtcNow);
            await _userRepository.AddAsync(user);

            await _messageBroker.PublishAsync(new SignedUp(user.Id, user.Email, user.Role));
        }
示例#3
0
        public async Task signup_endpoint_should_return_error_when_user_already_exists()
        {
            var id            = new AggregateId();
            var email         = "*****@*****.**";
            var fullname      = "fullname";
            var password      = "******";
            var role          = Role.User;
            var securityStamp = new Guid().ToString();

            // Add user
            var user = new User(id, email, fullname, "test.nl/image", password, role, securityStamp, 0, DateTime.MinValue, DateTime.UtcNow,
                                new string[] { });
            await _mongoDbFixture.InsertAsync(user.AsDocument());

            // Add user with same email
            var command = new SignUp(id, email, fullname, "test.nl/image", password, role, new string[] { });


            var response = await Act(command);

            response.Should().NotBeNull();
            response.StatusCode.Should().Be(HttpStatusCode.BadRequest);

            var content = await response.Content.ReadAsStringAsync();

            var exception = new EmailInUseException(email);

            content.Should().Contain(exception.Code);
        }
 public object Map(Exception exception, object message)
 {
     return(exception switch
     {
         EmailInUseException ex => new SignedUpRejected(ex.Email, ex.Message, ex.Code),
         InvalidCredentialsException ex => new SignInRejected(ex.Email, ex.Message, ex.Code),
         InvalidEmailException ex => message switch
         {
             SignIn command => new SignInRejected(command.Email, ex.Message, ex.Code),
             SignedUpRejected command => new SignedUpRejected(command.Email, ex.Message, ex.Code),
             _ => null
         },