public async Task <IActionResult> LoginAsync([FromBody] LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError(ModelState.ToString());
                var errors = ModelState.ToDictionary(x => x.Key, x => x.Value.Errors.Select(e => e.ErrorMessage).ToList())
                             .Select(x => new ValidationResponse()
                {
                    Key         = x.Key,
                    Validations = x.Value
                });
                return(BadRequest(errors));
            }

            var user = await _userManager.FindByNameAsync(model.UserName);

            if (user == null || !await _userManager.CheckPasswordAsync(user, model.Password))
            {
                var errors = new List <ValidationResponse>()
                {
                    new ValidationResponse
                    {
                        Key         = "UserName",
                        Validations = new List <string>()
                        {
                            "USER_INVADLID"
                        }
                    }
                };
                return(BadRequest(errors));
            }

            var claims = await BuildClaims(user);

            var token = _tokenService.GenerateAccessToken(claims);
            // Serialize and return the response
            var response = new AuthenResponseModel
            {
                Id = claims.FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.Sub) != null?claims.FirstOrDefault(x => x.Type == JwtRegisteredClaimNames.Sub).ToString() : string.Empty,
                         AuthenToken = token,
                         ExpiresIn   = (int)_jwtOptions.ValidFor.TotalSeconds,
                         Roles       = claims.Where(x => x.Type == ClaimTypes.Role).Select(x => x.Value).ToList(),
                         FullName    = user.FullName,
                         Email       = user.Email
            };

            if (model.IncludeRefreshToken)
            {
                var refreshToken = GenerateRefreshToken();
                user.RefreshTokenHash     = _userManager.PasswordHasher.HashPassword(user, refreshToken);
                response.RefreshTokenHash = user.RefreshTokenHash;
                await _userManager.UpdateAsync(user);
            }

            return(new OkObjectResult(response));
        }
        public async Task <IActionResult> LoginAsync([FromBody] LoginViewModel model)
        {
            if (!ModelState.IsValid)
            {
                _logger.LogError(ModelState.ToString());
                return(BadRequest(ModelState));
            }

            var user = await _userManager.FindByNameAsync(model.UserName);

            if (user == null)
            {
                return(BadRequest(
                           new ValidationResponse()
                {
                    Key = "UserName",
                    Validations = new List <string>()
                    {
                        "USER_NOT_FOUND"
                    }
                }));
            }
            var identity = await GetClaimsIdentity(model.UserName, model.Password);

            if (identity == null)
            {
                return(BadRequest(new ValidationResponse()
                {
                    Key = "Password",
                    Validations = new List <string>()
                    {
                        "PASSWORD_IS_CORECT"
                    }
                }));
            }

            var role = await _userManager.GetRolesAsync(user);

            identity.AddClaim(new Claim("fullName", user.FullName));
            identity.AddClaim(new Claim("email", user.Email));
            var token = await _jwtFactory.GenerateEncodedToken(model.UserName, identity);

            // Serialize and return the response
            var response = new AuthenResponseModel
            {
                Id          = identity.Claims.Single(c => c.Type == "id").Value,
                AuthenToken = token,
                ExpiresIn   = (int)_jwtOptions.ValidFor.TotalSeconds,
                Roles       = role.ToList(),
                FullName    = identity.Claims.Single(c => c.Type == "fullName").Value,
                Email       = user.Email
            };

            return(new OkObjectResult(response));
        }