UNPKG

@mridang/nestjs-auth

Version:

A comprehensive Auth.js integration for NestJS applications with TypeScript support, framework-agnostic HTTP adapters, and role-based access control

55 lines 2.48 kB
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; var __metadata = (this && this.__metadata) || function (k, v) { if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v); }; var __param = (this && this.__param) || function (paramIndex, decorator) { return function (target, key) { decorator(target, key, paramIndex); } }; import { Inject, Injectable } from '@nestjs/common'; import { HttpAdapterHost } from '@nestjs/core'; import { Auth, setEnvDefaults } from '@auth/core'; import { toHttpResponse, toWebRequest } from './utils/http-adapters.js'; import { AdapterFactory } from './utils/adapter.factory.js'; import { AuthModuleOptions } from './auth-module.options.js'; /** * Middleware that intercepts requests to Auth.js routes and processes * them through Auth.js core. Handles all authentication flows including * sign-in, sign-out, callbacks, and session management. */ let AuthMiddleware = class AuthMiddleware { options; adapterHost; httpAdapter; constructor(options, adapterHost) { this.options = options; this.adapterHost = adapterHost; } async use(req, res, next) { if (!this.httpAdapter) { this.httpAdapter = AdapterFactory.create(this.adapterHost); } const config = { ...this.options }; setEnvDefaults(process.env, config); try { const webRequest = toWebRequest(req, this.httpAdapter); const webResponse = await Auth(webRequest, config); await toHttpResponse(webResponse, res, this.httpAdapter); } catch (error) { next(error); } } }; AuthMiddleware = __decorate([ Injectable(), __param(0, Inject(AuthModuleOptions)), __param(1, Inject(HttpAdapterHost)), __metadata("design:paramtypes", [Object, HttpAdapterHost]) ], AuthMiddleware); export { AuthMiddleware }; //# sourceMappingURL=auth.middleware.js.map