npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@ng-catbee/jwt

v21.0.3

Published

A modern, type-safe Angular library for decoding, validating, and managing JSON Web Tokens (JWT) in client-side applications — fully compatible with Server-Side Rendering (SSR) and offering comprehensive token utilities including expiration tracking, clai

Downloads

111

Readme

@ng-catbee/jwt

Catbee JWT for Angular

A modern, type-safe Angular library for decoding, validating, and managing JSON Web Tokens (JWT) in client-side applications — fully compatible with Server-Side Rendering (SSR) and offering comprehensive token utilities including expiration tracking, claim extraction, and reactive observables.

📦 Demo

Stackblitz

✨ Features

  • 🔓 Token Decoding - Decode JWT headers and payloads with TypeScript support
  • Expiration Management - Check expiration, get remaining time, watch in real-time
  • 🎯 Type-Safe Claims - Extract specific claims with generic type support
  • Format Validation - Validate JWT format before decoding
  • 🔄 Reactive Observables - Watch token expiration with RxJS
  • 🌐 SSR Compatible - Works seamlessly with server-side rendering
  • 🚀 Zero Dependencies - Lightweight (except Angular and RxJS)

⚠️ Security Notice

This library decodes JWTs but does NOT verify signatures. Always verify JWT signatures on your backend server. Client-side decoding should only be used for reading non-sensitive metadata and UI logic.

🛠️ Installation

npm install @ng-catbee/jwt

⚡ Quick Start

import { Component, inject, signal, OnInit } from '@angular/core';
import { CatbeeJwtService, type JwtPayload } from '@ng-catbee/jwt';

interface UserPayload extends JwtPayload {
  userId: string;
  email: string;
  role: string;
}

@Component({
  selector: 'app-user',
  template: `
    @if(user) {
      <div>
        <h2>{{ user.email }}</h2>
        <p>Role: {{ user.role }}</p>
      </div>
   }
  `
})
export class UserComponent implements OnInit {
  private jwtService = inject(CatbeeJwtService);
  private authService = inject(AuthService);
  user = signal<UserPayload | null>(null);
  private token = this.authService.getToken(); // Replace with your token retrieval logic

  ngOnInit() {
    if (this.token && !this.jwtService.isExpired(this.token)) {
      this.user.set(this.jwtService.decodePayload<UserPayload>(this.token));
    }
  }
}

📚 API Reference

| Method | Description | |--------|-------------| | decodePayload<T>(token: string): T \| null | Decode JWT payload with type safety | | decode<T>(token: string): DecodedJwt<T> \| null | Decode complete JWT (header, payload, signature) | | isExpired(token: string, offsetSeconds?: number): boolean | Check if token is expired | | isValidFormat(token: string): boolean | Validate JWT format | | getExpirationDate(token: string): Date \| null | Get expiration as Date object | | getIssuedDate(token: string): Date \| null | Get issued-at as Date object | | getRemainingTime(token: string): number \| null | Get remaining seconds until expiration | | watchExpiry(token: string, tickMs: number): Observable<number> | Observe remaining time until expiration | | getClaim<T>(token: string, claim: string): T \| null | Extract specific claim with type safety |

🎯 Common Use Cases

Auth Guard

import { inject } from '@angular/core';
import { Router } from '@angular/router';
import { CatbeeJwtService } from '@ng-catbee/jwt';
import { AuthService } from './auth.service';

export const authGuard = () => {
  const jwtService = inject(CatbeeJwtService);
  const authService = inject(AuthService);
  const router = inject(Router);
  const token = authService.getToken(); // Replace with your token retrieval logic
  
  if (!token || !jwtService.isValidFormat(token) || jwtService.isExpired(token)) {
    return router.createUrlTree(['/login']);
  }
  
  return true;
};

function getAuthToken(): string | null {
  // Implement your secure token storage/retrieval here
  return null;
}

HTTP Interceptor

import { HttpInterceptorFn } from '@angular/common/http';
import { inject } from '@angular/core';
import { CatbeeJwtService } from '@ng-catbee/jwt';
import { AuthService } from './auth.service';

export const jwtInterceptor: HttpInterceptorFn = (req, next) => {
  const jwtService = inject(CatbeeJwtService);
  const authService = inject(AuthService);
  const token = authService.getToken(); // Replace with your token retrieval logic
  
  if (token && jwtService.isValidFormat(token) && !jwtService.isExpired(token)) {
    req = req.clone({
      setHeaders: { Authorization: `Bearer ${token}` }
    });
  }
  
  return next(req);
};

Watch Token Expiration

import { Component, inject, OnInit, OnDestroy, signal } from '@angular/core';
import { CatbeeJwtService } from '@ng-catbee/jwt';
import { Subject, takeUntil } from 'rxjs';
import { AuthService } from './auth.service';

@Component({
  selector: 'app-token-countdown',
  template: `<p>Token expires in: {{ remainingSeconds() }}s</p>`
})
export class TokenCountdownComponent implements OnInit, OnDestroy {
  private jwtService = inject(CatbeeJwtService);
  private authService = inject(AuthService);
  private destroy$ = new Subject<void>();
  private token = this.authService.getToken(); // Replace with your token retrieval logic
  public remainingSeconds = signal<number | null>(null);

  ngOnInit() {
    if (this.token) {
      this.jwtService.watchExpiry(this.token, 1000)
        .pipe(takeUntil(this.destroy$))
        .subscribe(remaining => this.remainingSeconds.set(remaining));
    }
  }

  ngOnDestroy() {
    this.destroy$.next();
    this.destroy$.complete();
  }
}

Role-Based Access

import { Injectable, inject } from '@angular/core';
import { CatbeeJwtService } from '@ng-catbee/jwt';

@Injectable({ providedIn: 'root' })
export class AuthService {
  private jwtService = inject(CatbeeJwtService);
  private token: string | null = null; // Store token in service state

  setToken(token: string) {
    this.token = token;
  }

  hasRole(requiredRole: string): boolean {
    if (!this.token) return false;
    
    const role = this.jwtService.getClaim<string>(this.token, 'role');
    return role === requiredRole;
  }

  hasPermission(permission: string): boolean {
    if (!this.token) return false;
    
    const permissions = this.jwtService.getClaim<string[]>(this.token, 'permissions');
    return permissions?.includes(permission) ?? false;
  }
}

📖 Documentation

💡 Full documentation available at https://catbee.in

📜 License

MIT © Catbee Technologies (see the LICENSE file for the full text)

🔗 Links