小编Ale*_*ler的帖子

如何从嵌套中的Request获取用户?

我无法从装饰器巢中的请求中获取用户,请帮助我。中间件工作良好,它通过令牌查找用户并将用户保存在请求我的中间件中:

import { Injectable, NestMiddleware, HttpStatus } from '@nestjs/common';
import { HttpException } from '@nestjs/common/exceptions/http.exception';
import { Request, Response } from 'express';
import { AuthenticationService } from '../modules/authentication-v1/authentication.service';

@Injectable()
export class AuthenticationMiddleware implements NestMiddleware {
    constructor(
        private readonly authenticationService : AuthenticationService
    ) {
    }
    async use(req: Request, res: Response, next: Function) {
        let token = req.headers;

        if(!token) {
            throw new HttpException('token is required', 401);
        }

        if (!token.match(/Bearer\s(\S+)/)) {
            throw new HttpException('Unsupported token', 401);
        }
        const [ tokenType, tokenValue ] = …
Run Code Online (Sandbox Code Playgroud)

javascript typescript nestjs

1
推荐指数
1
解决办法
4990
查看次数

标签 统计

javascript ×1

nestjs ×1

typescript ×1