Exemplo n.º 1
0
        public async Task <AuthenticationResponseModel> Handle(AuthenticationQuery request, CancellationToken cancellationToken)
        {
            await using var context = _contextFactory.CreateDbContext();

            var user = await context.Users.FirstOrDefaultAsync(u => u.UserName.ToLower() == request.Login.ToLower(), cancellationToken);

            if (user == null)
            {
                throw new AppException("Login or password is incorrect");
            }

            var passwordIsCorrect = _dataHashService.IsCorrectDataHash(request.Password, user.PasswordHash, user.PaswordSalt);

            if (!passwordIsCorrect)
            {
                throw new AppException("Login or password is incorrect");
            }

            if (!user.EmailConfirmed)
            {
                throw new AppException("Email not verified");
            }

            if (user.IsBlocked)
            {
                throw new AppException("The user is blocked");
            }

            await _mediator.Send(new UpdateActivityCommand(user.Id), cancellationToken);

            return(new AuthenticationResponseModel
            {
                Id = user.Id,
                UserName = user.UserName
            });
        }