Deploy via committed dist: local Nest build, server runs with .env
Build on the developer machine without Docker, commit dist/, and run on the server with Dockerfile.dist plus compose env_file. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
+11
@@ -0,0 +1,11 @@
|
||||
import { CanActivate, ExecutionContext } from '@nestjs/common';
|
||||
import { Reflector } from '@nestjs/core';
|
||||
import { JwtService } from '@nestjs/jwt';
|
||||
import { ConfigService } from '@nestjs/config';
|
||||
export declare class JwtAuthGuard implements CanActivate {
|
||||
private readonly reflector;
|
||||
private readonly jwtService;
|
||||
private readonly config;
|
||||
constructor(reflector: Reflector, jwtService: JwtService, config: ConfigService);
|
||||
canActivate(context: ExecutionContext): Promise<boolean>;
|
||||
}
|
||||
Vendored
+77
@@ -0,0 +1,77 @@
|
||||
"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);
|
||||
};
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
exports.JwtAuthGuard = void 0;
|
||||
const common_1 = require("@nestjs/common");
|
||||
const core_1 = require("@nestjs/core");
|
||||
const jwt_1 = require("@nestjs/jwt");
|
||||
const config_1 = require("@nestjs/config");
|
||||
const auth_decorators_1 = require("../decorators/auth.decorators");
|
||||
let JwtAuthGuard = class JwtAuthGuard {
|
||||
reflector;
|
||||
jwtService;
|
||||
config;
|
||||
constructor(reflector, jwtService, config) {
|
||||
this.reflector = reflector;
|
||||
this.jwtService = jwtService;
|
||||
this.config = config;
|
||||
}
|
||||
async canActivate(context) {
|
||||
const isPublic = this.reflector.getAllAndOverride(auth_decorators_1.IS_PUBLIC_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
if (isPublic)
|
||||
return true;
|
||||
const request = context.switchToHttp().getRequest();
|
||||
const authHeader = request.headers.authorization;
|
||||
if (!authHeader?.startsWith('Bearer ')) {
|
||||
throw new common_1.UnauthorizedException('توکن احراز هویت الزامی است');
|
||||
}
|
||||
const token = authHeader.slice(7);
|
||||
let payload;
|
||||
try {
|
||||
payload = await this.jwtService.verifyAsync(token, {
|
||||
secret: this.config.get('JWT_SECRET'),
|
||||
});
|
||||
}
|
||||
catch {
|
||||
throw new common_1.UnauthorizedException('توکن نامعتبر یا منقضی شده است');
|
||||
}
|
||||
const user = {
|
||||
id: payload.sub ?? payload.id,
|
||||
type: payload.type,
|
||||
role: payload.role,
|
||||
phone: payload.phone,
|
||||
};
|
||||
request.user = user;
|
||||
const authType = this.reflector.getAllAndOverride(auth_decorators_1.AUTH_TYPE_KEY, [context.getHandler(), context.getClass()]);
|
||||
if (authType && authType !== 'any' && user.type !== authType) {
|
||||
throw new common_1.ForbiddenException('دسترسی مجاز نیست');
|
||||
}
|
||||
const roles = this.reflector.getAllAndOverride(auth_decorators_1.ROLES_KEY, [
|
||||
context.getHandler(),
|
||||
context.getClass(),
|
||||
]);
|
||||
if (roles?.length && (!user.role || !roles.includes(user.role))) {
|
||||
throw new common_1.ForbiddenException('نقش کاربری کافی نیست');
|
||||
}
|
||||
return true;
|
||||
}
|
||||
};
|
||||
exports.JwtAuthGuard = JwtAuthGuard;
|
||||
exports.JwtAuthGuard = JwtAuthGuard = __decorate([
|
||||
(0, common_1.Injectable)(),
|
||||
__metadata("design:paramtypes", [core_1.Reflector,
|
||||
jwt_1.JwtService,
|
||||
config_1.ConfigService])
|
||||
], JwtAuthGuard);
|
||||
//# sourceMappingURL=jwt-auth.guard.js.map
|
||||
+1
@@ -0,0 +1 @@
|
||||
{"version":3,"file":"jwt-auth.guard.js","sourceRoot":"","sources":["../../../src/common/guards/jwt-auth.guard.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,2CAMwB;AACxB,uCAAyC;AACzC,qCAAyC;AACzC,2CAA+C;AAC/C,mEAIuC;AAIhC,IAAM,YAAY,GAAlB,MAAM,YAAY;IAEJ;IACA;IACA;IAHnB,YACmB,SAAoB,EACpB,UAAsB,EACtB,MAAqB;QAFrB,cAAS,GAAT,SAAS,CAAW;QACpB,eAAU,GAAV,UAAU,CAAY;QACtB,WAAM,GAAN,MAAM,CAAe;IACrC,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,OAAyB;QACzC,MAAM,QAAQ,GAAG,IAAI,CAAC,SAAS,CAAC,iBAAiB,CAAU,+BAAa,EAAE;YACxE,OAAO,CAAC,UAAU,EAAE;YACpB,OAAO,CAAC,QAAQ,EAAE;SACnB,CAAC,CAAC;QACH,IAAI,QAAQ;YAAE,OAAO,IAAI,CAAC;QAE1B,MAAM,OAAO,GAAG,OAAO,CAAC,YAAY,EAAE,CAAC,UAAU,EAAE,CAAC;QACpD,MAAM,UAAU,GAAG,OAAO,CAAC,OAAO,CAAC,aAAmC,CAAC;QACvE,IAAI,CAAC,UAAU,EAAE,UAAU,CAAC,SAAS,CAAC,EAAE,CAAC;YACvC,MAAM,IAAI,8BAAqB,CAAC,4BAA4B,CAAC,CAAC;QAChE,CAAC;QAED,MAAM,KAAK,GAAG,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;QAClC,IAAI,OAAyC,CAAC;QAC9C,IAAI,CAAC;YACH,OAAO,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,WAAW,CAAC,KAAK,EAAE;gBACjD,MAAM,EAAE,IAAI,CAAC,MAAM,CAAC,GAAG,CAAS,YAAY,CAAC;aAC9C,CAAC,CAAC;QACL,CAAC;QAAC,MAAM,CAAC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