public async Task <IActionResult> Post([FromBody] LoginViewModel login)
        {
            var user = await _userService.FindByEmail(login.Email);

            if (user == null)
            {
                return(BadRequest("Email ou senha inválido."));
            }

            if (_hash.CompareHash(login.Password, user.Password))
            {
                return(Ok(new
                {
                    Id = user.Id,
                    Name = user.Name,
                    Email = user.Email,
                    Token = _tokenGenerator.GenerateToken(),
                }));
            }

            return(BadRequest("Email ou senha inválido."));
        }
예제 #2
0
        public async Task <UserDTO> Update(UserDTO userDTO)
        {
            var userExists = await _userRepository.Get(userDTO.Id);

            if (userExists == null)
            {
                throw new ServiceException("Não foi encontrado nenhum usuário com ID informado!");
            }

            if (!(_hash.CompareHash(userDTO.OldPassword, userExists.Password)))
            {
                throw new ServiceException("Senha antiga incorreta!");
            }

            var user = _mapper.Map <User>(userDTO);

            user.Password = _hash.GenerateHash(user.Password);

            var userUpdate = await _userRepository.Update(user);

            return(_mapper.Map <UserDTO>(userUpdate));
        }