public UserAuthenticateResponseModel AuthenticateUser(UserAuthenticateRequestModel model)
        {
            // validation
            var results = _userValidator.Validate(model).ToArray();

            if (results.Length > 0)
            {
                throw new ValidationApiException(results);
            }

            // get the user from the repository / database
            var entity = _repo.GetUsers().SingleOrDefault(user => user.Email == model.Email && user.Password == model.Password);

            // throw unathorized exception if user doesn't exist
            if (entity == null)
            {
                throw new UnauthorizedApiException("Username or password is incorrect");
            }

            if (entity.Locked)
            {
                throw new UnauthorizedApiException("Account is locked");
            }

            // authentication successful so generate jwt token
            var token = GenerateJwtToken(entity.Id);

            //return the UserAuthenticateResponseModel to the controller
            return(_userMapper.AuthenticateMapper(entity, token));
        }
Esempio n. 2
0
        public IEnumerable <ValidationResult> Validate(UserAuthenticateRequestModel model)
        {
            var emailResult = ValidateEmail(model.Email);

            if (emailResult != null)
            {
                yield return(emailResult);
            }

            var passwordResult = ValidatePassword(model.Password);

            if (passwordResult != null)
            {
                yield return(passwordResult);
            }
        }
Esempio n. 3
0
        public async Task <ActionResult <UserAuthenticateResponseModel> > Authenticate(
            [FromBody] UserAuthenticateRequestModel model)
        {
            UserAuthenticateServiceModel userAuthenticateServiceModel = _mapper
                                                                        .Map <UserAuthenticateRequestModel, UserAuthenticateServiceModel>(model);
            UserTokenServiceModel userTokenServiceModel = await _userService
                                                          .AuthenticateAsync(userAuthenticateServiceModel);

            if (userAuthenticateServiceModel == null)
            {
                return(Unauthorized(new { message = "Username or password is incorrect" }));
            }

            UserAuthenticateResponseModel userAuthenticateResponseModel = _mapper
                                                                          .Map <UserTokenServiceModel, UserAuthenticateResponseModel>(userTokenServiceModel);

            return(Ok(userAuthenticateResponseModel));
        }
        public ActionResult <UserAuthenticateResponseModel> AuthenticateUser(UserAuthenticateRequestModel model)
        {
            var response = _userService.AuthenticateUser(model);

            return(Ok(response));
        }
Esempio n. 5
0
        public IActionResult Authenticate([FromBody] UserAuthenticateRequestModel loginRequest)
        {
            var authenticateResponse = _userService.Authenticate(_mapper.Map <User>(loginRequest));

            return(Ok(_mapper.Map <UserAuthenticateResponse>(authenticateResponse)));
        }