예제 #1
0
        public async Task <IActionResult> Add([FromBody] NetCoreUserRequest request)
        {
            try
            {
                var user = _mapper.Map <NetCoreUser>(request);
                user.Password = _passwordService.Hash(user.Password);
                var result = await _userService.Add(user);

                return(StatusCode(result.Status, result));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ha ocurrido un error al crear una transacción. Error: {ex.Message}");
                throw new Exception(ex.Message);
            }
        }
예제 #2
0
        public async Task <IActionResult> Authentication([FromBody] NetCoreUserRequest userLogin)
        {
            try
            {
                var result = await GetUser(userLogin);

                if (!result.Item1)
                {
                    return(BadRequest());
                }

                var token = GenerateToken(result.Item2);
                return(Ok(new { AccessToken = token }));
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ha ocurrido un error al crear una transacción. Error: {ex.Message}");
                throw new Exception(ex.Message);
            }
        }
예제 #3
0
        private async Task <(bool, NetCoreUser)> GetUser(NetCoreUserRequest request)
        {
            try
            {
                var entity   = _mapper.Map <NetCoreUser>(request);
                var response = await _userService.GetByUser(entity);

                if (!response.Item1)
                {
                    return(false, null);
                }

                var isValid = _passwordService.Check(response.Item2.Password, request.Password);

                return(isValid, response.Item2);
            }
            catch (Exception ex)
            {
                _logger.LogError($"Ha ocurrido un error al crear una transacción. Error: {ex.Message}");
                throw new Exception(ex.Message);
            }
        }