0.1.2 • Published 4 months ago

@tsdiapi/jwt-auth v0.1.2

Weekly downloads
-
License
MIT
Repository
github
Last release
4 months ago

@tsdiapi/jwt-auth

npm version License: MIT

TSDIAPI-JWT-Auth is a plugin for the TSDIAPI-Server framework that simplifies JWT-based authentication and authorization. It includes utilities for token creation, session validation, and declarative guards to secure API endpoints effectively.


Features

  • Token Management: Generate and verify JWT tokens with customizable payloads and expiration times.
  • Session Protection: Use built-in or custom session validation logic for secure API access.
  • Declarative Guards: Apply the @JWTGuard decorator to protect endpoints with optional validation handlers.
  • Custom Guards: Easily register and reference multiple guards to support various security requirements.
  • Environment Integration: Supports configuration through .env files to streamline deployment.

Installation

npm install @tsdiapi/jwt-auth

Or use the CLI to add the plugin:

tsdiapi plugins add jwt-auth

Usage

Register the Plugin

Import and register the plugin in your TSDIAPI application:

import createPlugin from "@tsdiapi/jwt-auth";
import { createApp } from "@tsdiapi/server";

createApp({
  plugins: [
    createPlugin({
      secretKey: "your-secret-key", // Use JWT_SECRET_KEY in .env as an alternative
      expirationTime: 60 * 60 * 24 * 7, // Token valid for 7 days, override with JWT_EXPIRATION_TIME in .env
    }),
  ],
});

Environment Configuration

Define environment variables in your .env file to avoid hardcoding sensitive data:

JWT_SECRET_KEY=your-secret-key
JWT_EXPIRATION_TIME=604800

Protecting Endpoints

Applying the @JWTGuard Decorator

Secure API endpoints using @JWTGuard. You can also add custom session validation logic:

import { JWTGuard } from "@tsdiapi/jwt-auth";
import { Controller, Get } from "routing-controllers";

@Controller("/profile")
export class ProfileController {
  @Get("/")
  @JWTGuard({
    validateSession: async (session) => {
      if (!session.userId) {
        return "User not authenticated!";
      }
      return true;
    },
  })
  getProfile() {
    return { message: "This is a protected route!" };
  }
}

Accessing the Current Session

Use the @CurrentSession decorator to retrieve the authenticated session inside your controller methods:

import { CurrentSession } from "@tsdiapi/jwt-auth";
import { Controller, Get } from "routing-controllers";

@Controller("/session")
export class SessionController {
  @Get("/")
  getSession(@CurrentSession() session: any) {
    return { session };
  }
}

Registering Custom Guards

You can register custom guards during plugin initialization. These guards can later be referenced by name:

createPlugin({
  secretKey: "your-secret-key",
  guards: {
    adminOnly: async (session) => {
      if (session.role !== "admin") {
        return "Only administrators are allowed!";
      }
      return true;
    },
  },
});

To use the custom guard:

import { JWTGuard } from "@tsdiapi/jwt-auth";
import { Controller, Get } from "routing-controllers";

@Controller("/admin")
export class AdminController {
  @Get("/dashboard")
  @JWTGuard({ validateSession: "adminOnly" })
  getDashboard() {
    return { message: "Welcome to the admin dashboard!" };
  }
}

Using the JWT Auth Provider

The JWTAuthProvider is the core service for handling JWT-based authentication in your TSDIAPI application. It provides methods for signing in users, verifying tokens, and managing guards for session validation.

Importing the Provider

To access the provider, import the getJWTAuthProvider function:

import { getJWTAuthProvider } from "@tsdiapi/jwt-auth";

Make sure the plugin is registered before calling this function, otherwise, an error will be thrown.


signIn(payload: Record<string, any>): Promise<string>

Generates a JWT token for the given user payload.

Example:

const authProvider = getJWTAuthProvider();

const token = await authProvider.signIn({
  userId: "123",
  role: "admin",
});
console.log("Generated Token:", token);

verify<T>(token: string): Promise<T | null>

Verifies a JWT token and returns the decoded session payload.

Example:

const authProvider = getJWTAuthProvider();

const session = await authProvider.verify<{ userId: string; role: string }>(token);
if (session) {
  console.log("Authenticated User:", session.userId);
} else {
  console.log("Invalid token");
}

API Reference

Plugin Options

OptionTypeDescription
secretKeystringSecret key for signing JWT tokens.
expirationTimenumberToken expiration time in seconds.
guardsRecord<string, ValidateSessionFunction>Custom guards for validating sessions.

License

This plugin is open-source and available under the MIT License.

0.0.1-alpha.16

4 months ago

0.0.1-alpha.15

4 months ago

0.1.0

4 months ago

0.1.2

4 months ago

0.1.1

4 months ago

0.0.2-alpha.1

4 months ago

0.0.1-alpha.14

5 months ago

0.0.1-alpha.13

5 months ago

0.0.1-alpha.12

5 months ago

0.0.1-alpha.11

5 months ago

0.0.1-alpha.10

5 months ago

0.0.1-alpha.8

5 months ago

0.0.1-alpha.9

5 months ago

0.0.1-alpha.7

5 months ago

0.0.1-alpha.6

5 months ago

0.0.1-alpha.5

5 months ago

0.0.1-alpha.4

5 months ago

0.0.1-alpha.3

5 months ago

0.0.1-alpha.2

6 months ago

0.0.1-alpha.1

6 months ago