JwtUtils: {
    decodeToken(token): JwtPayload;
    getPayloadClaim(token, claim): any;
    getTokenTimeRemaining(token): null | number;
    isJwt(token): boolean;
    isTokenExpiringSoon(): null | boolean;
    isTokenExpired(token): boolean;
    isValidToken(token, requiredClaims?): boolean;
} = ...

Type declaration

  • decodeToken:function
    • Decodes and parses a JWT token into its payload object.

      Parameters

      • token: string

        The JWT token string to decode

      Returns JwtPayload

      The decoded JWT payload containing all claims

      Throws

      Error if token is invalid, malformed, or cannot be parsed

      Example

      const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';
      const payload = JwtUtils.decodeToken(token);
      console.log(payload.sub); // '1234567890'
      console.log(payload.exp); // 1234567890
  • getPayloadClaim:function
    • Extracts a specific claim value from a JWT token's payload.

      Parameters

      • token: string

        The JWT token string to parse

      • claim: string

        The name of the claim to retrieve

      Returns any

      The claim value or undefined if the claim is not found

      Throws

      Error if token is invalid or cannot be parsed

      Example

      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"
  • getTokenTimeRemaining:function
    • Calculates the time remaining until a JWT token expires.

      Parameters

      • token: string

        The JWT token to check for expiration

      Returns null | number

      The time remaining in seconds, or null if no expiration time is set

      Throws

      Error if token is invalid or cannot be parsed

      Example

      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');
      }
  • isJwt:function
    • Validates if a string follows the correct JWT token format (three base64url-encoded parts).

      Parameters

      • token: string

        The string to validate as a JWT token

      Returns boolean

      True if the string follows JWT format, false otherwise

      Example

      const validToken = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.signature';
      const invalidToken = 'not.a.jwt.token';

      JwtUtils.isJwt(validToken); // true
      JwtUtils.isJwt(invalidToken); // false
      JwtUtils.isJwt('invalid'); // false
  • isTokenExpiringSoon:function
    • Checks if the JWT token stored in localStorage will expire within the next hour.

      Returns null | boolean

      True if token will expire within an hour, false if not expiring soon, null if no token exists

      Example

      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');
      }
  • isTokenExpired:function
    • Determines if a JWT token has expired based on its expiration time claim.

      Parameters

      • token: string

        The JWT token string to check

      Returns boolean

      True if the token is expired, false if still valid or has no expiration

      Throws

      Error if token is invalid or cannot be parsed

      Example

      const token = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9...';

      if (JwtUtils.isTokenExpired(token)) {
      console.log('Token has expired - please authenticate again');
      } else {
      console.log('Token is still valid');
      }
  • isValidToken:function
    • Performs comprehensive validation of a JWT token including expiration and required claims.

      Parameters

      • token: string

        The JWT token string to validate

      • requiredClaims: string[] = []

        Array of claim names that must be present in the token

      Returns boolean

      True if token is valid, not expired, and contains all required claims

      Throws

      Error if token is invalid or cannot be parsed

      Example

      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');
      }