@nxtoai/jwtette
v1.0.13
Published
JWT authentication package for NxtoAI microservices
Downloads
47
Maintainers
Readme
@nxtoai/jwtette
JWT authentication package for NxtoAI microservices.
Installation
npm install @nxtoai/jwtetteUsage
- Import the module in your NestJS application:
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 {}- Use the AuthMiddleware in your application:
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('*');
}
}- Use JwtHelper in your services:
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 validationaerospike: AeroSpike configuration for token caching
Environment Variables
JWT_SECRET: JWT secret keyJWT_EXPIRES_IN: JWT token expiration timeAEROSPIKE_HOSTS: Comma-separated list of AeroSpike hostsAEROSPIKE_PORT: AeroSpike portAEROSPIKE_NAMESPACE: AeroSpike namespaceAEROSPIKE_TIMEOUT: AeroSpike timeoutAEROSPIKE_MAX_SOCKETS: Maximum number of socketsAEROSPIKE_MAX_CONNS: Maximum number of connections per nodeAEROSPIKE_USER: AeroSpike usernameAEROSPIKE_PASSWORD: AeroSpike password
