I have this bit of code on the c# .NET server side that returns token to the Angular2 client. Is there any documentation describing how to interpret that on the java script angular side (which classes and its members to use to parse it properly)
private string generateJwtToken(Account account)
{
var tokenHandler = new JwtSecurityTokenHandler();
var key = Encoding.ASCII.GetBytes(_appSettings.Secret);
var tokenDescriptor = new SecurityTokenDescriptor
{
Subject = new ClaimsIdentity(new[] { new Claim(/* "id" */ClaimTypes.NameIdentifier, account.Id.ToString()) }),
Expires = DateTime.UtcNow.AddMinutes(15),
SigningCredentials = new SigningCredentials(new SymmetricSecurityKey(key), SecurityAlgorithms.HmacSha256Signature)
};
var token = tokenHandler.CreateToken(tokenDescriptor);
return tokenHandler.WriteToken(token);
}
Client side parsing:
var exp = this.accountValue.jwtToken.split('.')[1];
const jwtToken = JSON.parse(atob(this.accountValue.jwtToken.split('.')[1]));
// set a timeout to refresh the token a minute before it expires
const expiresUtc = new Date(jwtToken.exp);
const expires = new Date(jwtToken.exp * 1000);
const mins = expires.getTime() - Date.now();
const timeout = expires.getTime() - Date.now() - (60 * 1000);
this.refreshTokenTimeout = setTimeout(() => this.refreshToken().subscribe(), timeout);