@legalflow/auth
v1.1.0
Published
Shared JWT auth utilities for LegalFlow
Readme
@legalflow/auth
Shared JWT authentication utilities for LegalFlow microservices.
Installation
npm install @legalflow/authUsage
This package provides utilities for JWT token creation, verification, and Fastify middleware for authentication.
Environment Variables
Make sure to set the SUPABASE_JWT_SECRET environment variable:
export SUPABASE_JWT_SECRET=your-jwt-secretCreating Internal Tokens
import { createInternalToken } from '@legalflow/auth';
const token = createInternalToken('ai-service', 'jobs-service', '1h');
console.log('Internal token:', token);Using the Fastify Middleware
import { createAuthMiddleware } from '@legalflow/auth';
import Fastify from 'fastify';
const fastify = Fastify();
// Add the auth middleware to verify tokens
fastify.addHook('onRequest', createAuthMiddleware('jobs-service'));
// Your routes will now require authentication
fastify.get('/protected', async (request, reply) => {
// jwtPayload is attached to the request by the middleware
const payload = request.jwtPayload;
return { message: 'Protected route', payload };
});API Reference
Types
InternalJwtPayload: Interface for internal service-to-service JWT payloadsUserJwtPayload: Interface for user authentication JWT payloads
Functions
createInternalToken(issuer, audience, expiresIn?, userId?): Creates an internal token for service-to-service callsverifyToken<T>(token): Verifies any token (internal/user)createAuthMiddleware(expectedAudience): Creates a Fastify hook that verifies the token audience
