1.0.13 • Published 11 months ago

@nxtoai/jwtette v1.0.13

Weekly downloads
-
License
ISC
Repository
-
Last release
11 months ago

@nxtoai/jwtette

JWT authentication package for NxtoAI microservices.

Installation

npm install @nxtoai/jwtette

Usage

  1. 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 {}
  1. 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('*');
  }
}
  1. 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 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
1.0.13

11 months ago

1.0.12

11 months ago

1.0.11

11 months ago

1.0.10

11 months ago

1.0.9

11 months ago

1.0.8

11 months ago

1.0.7

11 months ago

1.0.6

11 months ago

1.0.5

11 months ago

1.0.4

11 months ago

1.0.3

11 months ago

1.0.2

11 months ago

1.0.1

11 months ago

1.0.0

11 months ago