Ejemplo n.º 1
0
        public async Task <IActionResult> Login([FromBody] LoginRequest request)
        {
            if (request == null)
            {
                return(BadRequest());
            }

            try
            {
                User user  = _userService.Authenticate(request.UserName, request.Password);
                var  token = _jwtProvider.WriteToken(user, request.RememberMe);

                return(Ok(token));
            }
            catch (UserNotExistException)
            {
                ModelState.AddModelError(nameof(request.UserName), $"User: {request.UserName} doesn't exist in Database");
                return(BadRequest(ModelState));
            }
            catch (BadPasswordException)
            {
                ModelState.AddModelError(nameof(request.Password), $"Wrong password");
                return(BadRequest(ModelState));
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                throw;
            }
        }