UNPKG

nest-phylax

Version:

Security library for NestJS

106 lines 4.62 kB
"use strict"; 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); } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.RefreshTokenController = exports.PasswordLoginController = void 0; const common_1 = require("@nestjs/common"); const jwt_auth_service_1 = require("../services/jwt.auth.service"); const types_1 = require("../types"); const jsonwebtoken_1 = require("jsonwebtoken"); const decorators_1 = require("../../decorators"); let PasswordLoginController = class PasswordLoginController { constructor(jwtAuthService) { this.jwtAuthService = jwtAuthService; } async passwordLogin(dto) { validatePasswordLoginDto(dto); const { accessToken, refreshToken } = await this.jwtAuthService.passwordLogin(dto); return { message: 'Login successful', data: { accessToken: accessToken, refreshToken: refreshToken, }, }; } }; __decorate([ (0, common_1.Post)(), __param(0, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [types_1.PasswordLoginDto]), __metadata("design:returntype", Promise) ], PasswordLoginController.prototype, "passwordLogin", null); PasswordLoginController = __decorate([ (0, common_1.Controller)('/password-login'), (0, decorators_1.Public)(), __metadata("design:paramtypes", [jwt_auth_service_1.JwtAuthService]) ], PasswordLoginController); exports.PasswordLoginController = PasswordLoginController; function validatePasswordLoginDto(dto) { if (!(dto === null || dto === void 0 ? void 0 : dto.email)) { throw new common_1.BadRequestException('Email is required'); } if (!(dto === null || dto === void 0 ? void 0 : dto.password)) { throw new common_1.BadRequestException('Password is required'); } if (typeof dto.email !== 'string') { throw new common_1.BadRequestException('Email must be a string'); } if (typeof dto.password !== 'string') { throw new common_1.BadRequestException('Password must be a string'); } if (!/^\w+([.-]?\w+)*@\w+([.-]?\w+)*(\.\w{2,3})+$/.test(dto.email)) { throw new common_1.BadRequestException('Invalid email'); } } let RefreshTokenController = class RefreshTokenController { constructor(jwtAuthService) { this.jwtAuthService = jwtAuthService; } async refreshToken(dto) { validateRefreshTokenDto(dto); const { accessToken } = await this.jwtAuthService.refreshToken(dto.refreshToken); return { message: 'Refresh token successful', data: { accessToken: accessToken, }, }; } }; __decorate([ (0, common_1.Post)(), __param(0, (0, common_1.Body)()), __metadata("design:type", Function), __metadata("design:paramtypes", [Object]), __metadata("design:returntype", Promise) ], RefreshTokenController.prototype, "refreshToken", null); RefreshTokenController = __decorate([ (0, common_1.Controller)('/refresh-token'), (0, decorators_1.Public)(), __metadata("design:paramtypes", [jwt_auth_service_1.JwtAuthService]) ], RefreshTokenController); exports.RefreshTokenController = RefreshTokenController; function validateRefreshTokenDto(dto) { if (!(dto === null || dto === void 0 ? void 0 : dto.refreshToken)) { throw new common_1.BadRequestException('Refresh token is required'); } try { (0, jsonwebtoken_1.decode)(dto.refreshToken); } catch (error) { throw new common_1.BadRequestException('Invalid refresh token'); } } //# sourceMappingURL=index.js.map