11.22.2 • Published 8 months ago

auth-interceptor-lib v11.22.2

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

AuthInterceptor

Middleware for authentication using a token provider. This middleware validates the authentication token by comparing it with the defined protected routes. Additionally, it uses a global logger to record information about unauthorized requests.

Parameters

TokenProvider

  • Type: AuthProviderInterface
  • Description: Implementation of the token provider. Examples of implementations include CognitoProvider.

ConsoleLogger

  • Type: LoggerInterface
  • Description: Logger instance used to record logs in the system.

Functionality

  1. Initializes the global logger using LoggerSingleton.initialize(ConsoleLogger).
  2. Checks if the current route is a public route. If it is, access is allowed directly.
  3. Validates the token provided in the authorization header.
  4. If the token is invalid:
    • Uses the logger to record the unauthorized attempt.
    • Returns a response with the status 401 Unauthorized.
  5. If the token is valid:
    • Allows the request to continue by calling next().

Return

A middleware function compatible with frameworks like Express.

Usage Example with Express

import ConsoleLogger from './logger/console.logger';
import express from 'express';
import AuthInterceptor, { CognitoProvider } from 'auth-interceptor-lib';

const app = express();

// Initialize the global logger, type LoggerInterface exported by: import { LoggerInterface } from 'auth-interceptor-lib'
const logger = new ConsoleLogger();

// Initialize the AuthInterceptor with the TokenProvider and Logger
const authMiddleware = AuthInterceptor(new CognitoProvider(), logger);

// Use the middleware in the application
app.use(authMiddleware);

Usage Example with NestJS

import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'
import AuthProvider, { CognitoProvider } from 'auth-interceptor-lib';
import { ConsoleLogger } from '@/@core/infra/middlewares/logger';

@Module({
  imports: [],
  providers: [],
})

export class AppModule implements NestModule {
  configure(consumer: MiddlewareConsumer) {
    const userPoolId = process.env.USER_POOL_ID || '';
    if (!userPoolId) {
      throw new Error('USER_POOL_ID is not set in the environment variables.');
    }
    consumer
      .apply(AuthProvider(new CognitoProvider(userPoolId), new ConsoleLogger()))
      .forRoutes('*');
  }
}
9.12345.1

8 months ago

9.10123.1

8 months ago

2.2.2

8 months ago

10.22.2

8 months ago

9.555.1

8 months ago

9.912345.1

8 months ago

11.22.2

8 months ago

9.12.1

8 months ago

9.1234567.1

8 months ago

9.123456.1

8 months ago

9.101.1

8 months ago

9.7787.1

8 months ago

9.1.1

8 months ago

9.1012.1

8 months ago

9.9123456.1

8 months ago

9.9123.1

8 months ago

9.9.1

8 months ago

9.444.1

8 months ago

9.77822227.1

8 months ago

2.22.2

8 months ago

10.23.2

8 months ago

9.1234.1

8 months ago

9.12345678.1

8 months ago

9.91234.1

8 months ago

9.91.1

8 months ago

9.912.1

8 months ago

9.123.1

8 months ago

10.33.2

8 months ago

9.777.1

8 months ago

9.123456789.1

8 months ago

11.1.2

8 months ago

6.1.0

8 months ago

4.7.8

8 months ago

6.2.41

8 months ago

6.0.0

8 months ago

6.3.0

8 months ago

6.2.1

8 months ago

6.1.1

8 months ago

6.2.0

8 months ago

4.217.8

8 months ago

6.2.3

8 months ago

4.17.8

8 months ago

6.2.2

8 months ago

3.3.8

8 months ago

0.12.1

8 months ago

0.31234.1

8 months ago

3.7.8

8 months ago

3.5.8

8 months ago

0.31.1

8 months ago

0.312345.1

8 months ago

0.312.1

8 months ago

6.2.4

8 months ago

0.1.1

8 months ago

6.2.6

8 months ago

6.2.9

8 months ago

6.2.8

8 months ago

0.3123.1

8 months ago

0.3.1

8 months ago

4.317.8

8 months ago

2.3.8

8 months ago

2.2.8

8 months ago

1.2.8

8 months ago

1.1.8

8 months ago

1.1.4

8 months ago

1.1.3

8 months ago

1.1.2

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0

8 months ago