Exemplo n.º 1
0
        public async Task HandleAsync(SignInCommand command)
        {
            var user = await _userRepository.GetAsync(command.Email) ?? throw new InvalidCredentialsException();

            if (!user.IsVerified)
            {
                throw new InvalidCredentialsException();
            }

            var result = _passwordHasher.VerifyHashedPassword(user, user.PasswordHash, command.Password);

            if (result == PasswordVerificationResult.Failed)
            {
                throw new InvalidCredentialsException();
            }

            var authDto = _authTokenService.Create(user.Id);

            _tokensCache.Set(user.Email, authDto);
        }
Exemplo n.º 2
0
        public async Task HandleAsync(SignIn command)
        {
            var user = await _repository.GetAsync(u => u.Username == command.Username);

            if (user is null)
            {
                throw new UserNotFoundException(command.Username);
            }

            var passwordHash = _passwordService.HashPassword(command.Password, user.Salt);

            if (passwordHash != user.Password)
            {
                throw new InvalidUserPasswordException(user.Username);
            }

            var token        = _authTokensService.Create(user.Username);
            var refreshToken = await _refreshTokensService.CreateAsync(user.Username);

            token.RefreshToken = refreshToken;
            _cache.Set(token);
        }