public async Task <string> Handle(SignInCommand request, CancellationToken cancellationToken)
        {
            var password = _hashPassword.Hash(request.Password);
            var user     = await _userRepository.GetByLoginPasswordAsync(request.Login, password)
                           ?? throw new AuthenticationNotValidException("The login with password does not match");

            return(await _tokenGenerator.GenerateAsync(user));
        }
Exemple #2
0
        public async Task <string> Handle(RegisterCommand request, CancellationToken cancellationToken)
        {
            if (await _userRepository.IsUsernameExistsAsync(request.Username))
            {
                throw new UsernameAlreadyExistsException($"Username {request.Username} already exists");
            }

            if (await _userRepository.IsEmailExistsAsync(request.Email))
            {
                throw new EmailAlreadyExistsException($"the email {request.Email} already exists");
            }

            var user = new User(request.UserId,
                                request.Username,
                                request.Email,
                                _hashPassword.Hash(request.Password),
                                _dateTimeProvider.Now);
            await _userRepository.SaveAsync(user);

            return(await _tokenGenerator.GenerateAsync(user));
        }