Const
The JWT token string to decode
The decoded JWT payload containing all claims
Error if token is invalid, malformed, or cannot be parsed
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
const payload = JwtUtils.decodeToken(token);
console.log(payload.sub); // '1234567890'
console.log(payload.exp); // 1234567890
Extracts a specific claim value from a JWT token's payload.
The JWT token string to parse
The name of the claim to retrieve
The claim value or undefined if the claim is not found
Error if token is invalid or cannot be parsed
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
const userId = JwtUtils.getPayloadClaim(token, 'sub'); // "1234567890"
const role = JwtUtils.getPayloadClaim(token, 'role'); // "admin"
const email = JwtUtils.getPayloadClaim(token, 'email'); // "user@example.com"
Calculates the time remaining until a JWT token expires.
The JWT token to check for expiration
The time remaining in seconds, or null if no expiration time is set
Error if token is invalid or cannot be parsed
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
const timeRemaining = JwtUtils.getTokenTimeRemaining(token);
if (timeRemaining !== null) {
console.log(`Token expires in ${timeRemaining} seconds`);
} else {
console.log('Token has no expiration time');
}
Validates if a string follows the correct JWT token format (three base64url-encoded parts).
The string to validate as a JWT token
True if the string follows JWT format, false otherwise
const validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signature';
const invalidToken = 'not.a.jwt.token';
JwtUtils.isJwt(validToken); // true
JwtUtils.isJwt(invalidToken); // false
JwtUtils.isJwt('invalid'); // false
Checks if the JWT token stored in localStorage will expire within the next hour.
True if token will expire within an hour, false if not expiring soon, null if no token exists
const result = JwtUtils.isTokenExpiringSoon();
if (result === null) {
console.log('No token found in localStorage');
} else if (result) {
console.log('Token expires soon - consider refreshing');
} else {
console.log('Token is still valid for more than an hour');
}
Determines if a JWT token has expired based on its expiration time claim.
The JWT token string to check
True if the token is expired, false if still valid or has no expiration
Error if token is invalid or cannot be parsed
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
if (JwtUtils.isTokenExpired(token)) {
console.log('Token has expired - please authenticate again');
} else {
console.log('Token is still valid');
}
Performs comprehensive validation of a JWT token including expiration and required claims.
The JWT token string to validate
Array of claim names that must be present in the token
True if token is valid, not expired, and contains all required claims
Error if token is invalid or cannot be parsed
const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
// Basic validation (just check expiration)
const isValid = JwtUtils.isValidToken(token);
// Validation with required claims
const isValidWithClaims = JwtUtils.isValidToken(token, ['sub', 'role', 'email']);
if (isValidWithClaims) {
console.log('Token is valid and has all required claims');
}
Decodes and parses a JWT token into its payload object.