Example #1
0
        public async Task <IActionResult> Authenticate([FromBody] LoginDto model)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest());
            }

            var user = await _userService.FindUserByEmail(model);

            if (user == null)
            {
                return(BadRequest());
            }

            PasswordVerificationResult result = _userService.VerifyHashedPassword(user, model.Password);

            var signed = await _userManager.PasswordSignInAsync(model.Email, model.Password, isPersistent : false, lockoutOnFailure : false);

            if (result != PasswordVerificationResult.Success || !signed.Succeeded)
            {
                return(BadRequest());
            }

            JwtSecurityToken token = await _userService.GetJwtSecurityToken(user);

            LoggedInUser loggedInUser = _mapper.Map <LoggedInUser>(user);

            return(Ok(new
            {
                token = new JwtSecurityTokenHandler().WriteToken(token),
                expiration = token.ValidTo,
                user = loggedInUser
            }));
        }