JWT Access Token Expiry Checker (shared/jwt.ts)

vibe-kanban · shared/jwt.ts · typescript · Apache-2.0
auth, security, lang:typescript

Decodes JWT access tokens and checks if they are approaching expiration. Reuse when implementing automatic token refresh logic in web clients.

// Source: https://github.com/BloopAI/vibe-kanban | License: Apache-2.0 | Copyright You must retain, in the Source form of any Derivative Works
// Provided via justclaw.it.com; keep this notice with the code.
import { jwtDecode } from 'jwt-decode';

type AccessTokenClaims = {
  exp: number;
  aud: string;
};

const TOKEN_REFRESH_LEEWAY_MS = 20_000;
const ACCESS_TOKEN_AUD = 'access';

const getTokenExpiryMs = (token: string): number | null => {
  try {
    const { exp, aud } = jwtDecode<AccessTokenClaims>(token);
    if (aud !== ACCESS_TOKEN_AUD) return null;
    if (!Number.isFinite(exp)) return null;
    return exp * 1000;
  } catch {
    return null;
  }
};

export const shouldRefreshAccessToken = (token: string): boolean => {
  const expiresAt = getTokenExpiryMs(token);
  if (expiresAt 

... (truncated -- full source via MCP)

See the full source, get the GitHub permalink, and search for more like it.

Get a free API key