Example #1
0
        public async Task <IActionResult> Post([FromForm] AuthenticateUserCommand command)
        {
            if (command == null)
            {
                return(await ApiResponse(null, new List <Notification> {
                    new Notification("User", "Usuário ou senha inválidos.")
                }));
            }

            var identity = await GetClaims(command);

            if (identity == null)
            {
                return(await ApiResponse(null, new List <Notification> {
                    new Notification("User", "Usuário ou senha inválidos.")
                }));
            }

            //Basic user claims
            var claims = new List <Claim>()
            {
                new Claim(JwtRegisteredClaimNames.UniqueName, command.Username),
                new Claim(JwtRegisteredClaimNames.NameId, command.Username),
                new Claim(JwtRegisteredClaimNames.Email, command.Username),
                new Claim(JwtRegisteredClaimNames.Sub, command.Username),
                new Claim(JwtRegisteredClaimNames.Jti, await _tokenOptions.JitGenerator()),
                new Claim(JwtRegisteredClaimNames.Iat, ToUnixEpochDate(_tokenOptions.IssuedAt).ToString(), ClaimValueTypes.Integer64)
            };

            //User specific claims
            claims.AddRange(identity.FindAll("ModernStore"));

            var jwt = new JwtSecurityToken(
                issuer: _tokenOptions.Issuer,
                audience: _tokenOptions.Audience,
                claims: claims.AsEnumerable(),
                notBefore: _tokenOptions.NotBefore,
                expires: _tokenOptions.Expiration,
                signingCredentials: _tokenOptions.SigningCredentials);

            var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

            return(Ok(new
            {
                token = encodedJwt,
                expires = (int)_tokenOptions.ValidFor.TotalSeconds,
                user = new
                {
                    id = _customer.Id,
                    name = _customer.Name.ToString(),
                    email = _customer.Email.Address,
                    username = _customer.User.Username
                }
            }));
        }