????

Your IP : 3.138.151.175


Current Path : C:/inetpub/vhost/invest.gdtsolutions.vn/api/dist/apps/auth/
Upload File :
Current File : C:/inetpub/vhost/invest.gdtsolutions.vn/api/dist/apps/auth/auth.guard.js

"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.AuthGuard = void 0;
const common_1 = require("@nestjs/common");
const jwt_1 = require("@nestjs/jwt");
const config_1 = require("@nestjs/config");
const token_service_1 = require("./token.service");
const users_1 = require("../users");
let AuthGuard = class AuthGuard {
    constructor(jwtService, tokenService, configService, userService) {
        this.jwtService = jwtService;
        this.tokenService = tokenService;
        this.configService = configService;
        this.userService = userService;
    }
    async canActivate(context) {
        const request = context.switchToHttp().getRequest();
        const token = this.tokenService.extractTokenFromHeader(request);
        if (!token) {
            throw new common_1.UnauthorizedException('Người dùng chưa đăng nhập hệ thống!');
        }
        try {
            const tokenPayload = await this.tokenService.getTokenPayload(token);
            const decodedToken = await this.jwtService.verifyAsync(tokenPayload, {
                secret: this.configService.get('ACCESS_TOKEN_SECRET'),
            });
            const { userId } = decodedToken;
            const user = await this.userService.getUserPermissions(userId);
            if (!user) {
                throw new common_1.UnauthorizedException('Tài khoản người dùng không tồn tại!');
            }
            request['userId'] = user.id;
        }
        catch (err) {
            throw new common_1.UnauthorizedException(err.message);
        }
        return true;
    }
};
exports.AuthGuard = AuthGuard;
exports.AuthGuard = AuthGuard = __decorate([
    (0, common_1.Injectable)(),
    __metadata("design:paramtypes", [jwt_1.JwtService,
        token_service_1.TokenService,
        config_1.ConfigService,
        users_1.UsersService])
], AuthGuard);
//# sourceMappingURL=auth.guard.js.map