UNPKG

@nxtoai/jwtette

Version:

JWT authentication package for NxtoAI microservices

101 lines (81 loc) 2.39 kB
# @nxtoai/jwtette JWT authentication package for NxtoAI microservices. ## Installation ```bash npm install @nxtoai/jwtette ``` ## Usage 1. Import the module in your NestJS application: ```typescript import { JwtetteModule } from '@nxtoai/jwtette'; @Module({ imports: [ JwtetteModule.forRoot({ secret: process.env.JWT_SECRET, expiresIn: '24h', skipJwtEndpoints: [ '/auth/login', 'POST:/auth/login', '/auth/register', 'POST:/auth/register' ], aerospike: { hosts: ['localhost'], namespace: 'test', port: 3000, timeout: 1000, maxSockets: 100, maxConnsPerNode: 100 } }) ] }) export class AppModule {} ``` 2. Use the AuthMiddleware in your application: ```typescript import { Module, NestModule, MiddlewareConsumer } from '@nestjs/common'; import { AuthMiddleware } from '@nxtoai/jwtette'; @Module({}) export class AppModule implements NestModule { configure(consumer: MiddlewareConsumer) { consumer .apply(AuthMiddleware) .forRoutes('*'); } } ``` 3. Use JwtHelper in your services: ```typescript import { Injectable } from '@nestjs/common'; import { JwtHelper } from '@nxtoai/jwtette'; @Injectable() export class AuthService { constructor(private readonly jwtHelper: JwtHelper) {} async login(userId: string) { const token = this.jwtService.sign({ userId }); await this.jwtHelper.cacheToken(token, userId); return token; } async logout(token: string) { await this.jwtHelper.invalidateToken(token); } } ``` ## Configuration The module accepts the following configuration options: - `secret`: JWT secret key (default: process.env.JWT_SECRET) - `expiresIn`: JWT token expiration time (default: '24h') - `skipJwtEndpoints`: Array of endpoints to skip JWT validation - `aerospike`: AeroSpike configuration for token caching ## Environment Variables - `JWT_SECRET`: JWT secret key - `JWT_EXPIRES_IN`: JWT token expiration time - `AEROSPIKE_HOSTS`: Comma-separated list of AeroSpike hosts - `AEROSPIKE_PORT`: AeroSpike port - `AEROSPIKE_NAMESPACE`: AeroSpike namespace - `AEROSPIKE_TIMEOUT`: AeroSpike timeout - `AEROSPIKE_MAX_SOCKETS`: Maximum number of sockets - `AEROSPIKE_MAX_CONNS`: Maximum number of connections per node - `AEROSPIKE_USER`: AeroSpike username - `AEROSPIKE_PASSWORD`: AeroSpike password