1.0.13 • Published 8 months ago

@nxtoai/jwtette v1.0.13

Weekly downloads
-
License
ISC
Repository
-
Last release
8 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

8 months ago

1.0.12

8 months ago

1.0.11

8 months ago

1.0.10

8 months ago

1.0.9

8 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

8 months ago

1.0.5

8 months ago

1.0.4

8 months ago

1.0.3

8 months ago

1.0.2

8 months ago

1.0.1

8 months ago

1.0.0

8 months ago