如何将 Auth0 与 typescript-express-decorators 一起使用

how to use Auth0 with typescript-express-decorators

我正在使用 typescript-express-decorators (ts.ed) library to build my restful apis. I would like to integrate it with a Auth0 authentication workflow. But I have no idea how to proceed. On the ts.ed pages, there is an example of how to setup authentication using passport.js 和中间件。

Auth0 的 Getting started with Node.js 表明我应该构建一个快速中间件 (express-jwt),它检查请求中包含的有效用户访问令牌。然后我应该将这个中间件添加到每个安全路由。

这是此类设置的屏幕截图(来自 "Getting Started" 部分):

所以我想我的问题是:如何创建自定义中间件并将其附加到我想要保护的每条路由?

正如我所说,我查看了 ts.ed 的文档。并且只有 passport.js 的示例(使用中间件和经过身份验证的装饰器)。

希望ts.ed看到这个问题并给予指导

您可以使用带有 @Use 装饰器的任何中间件。例如:

import { Controller, Use, Get, Req, Res, Next } from '@tsed/common'
import { Request, Response, NextFunction } from 'express'
import permitAuth from '../middlewares/permitAuth'

@Controller('/account')
export default class AccountController {
    @Get('/userInfo')
    @Use(permitAuth())
    async userInfoGet(@Req() req: Request, @Res() res: Response, @Next() next: NextFunction) {
        return res.json(req.user.toJSON())
    }
}

您也可以在控制器级别使用它:

@Controller('/my-controller')
@Use(myMiddleware)
export default class MyController {
    ...
}

http://tsed.io/docs/controllers.html#custom-middleware