【问题标题】:how to use Auth0 with typescript-express-decorators如何将 Auth0 与 typescript-express-decorators 一起使用
【发布时间】:2019-09-23 05:30:31
【问题描述】:

我正在使用typescript-express-decorators (ts.ed) 库来构建我的restful api。我想将它与Auth0 身份验证工作流集成。但我不知道如何进行。在 ts.ed 页面上,有一个如何使用passport.js 和中间件设置身份验证的示例。

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

以下是此类设置的屏幕截图(来自“入门”部分):

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

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

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

【问题讨论】:

  • 嗨。你解决了这个问题吗。我编写了自定义装饰器和其他中间件工作,但我对 express-jwt 库有疑问
  • 你好,我已经搬到了 graphql,不再使用“ts.ed”库

标签: typescript express decorator middleware auth0


【解决方案1】:

您可以使用任何带有@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

【讨论】:

    猜你喜欢
    • 2012-09-24
    • 2021-06-18
    • 1970-01-01
    • 2020-04-22
    • 2018-05-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-04
    相关资源
    最近更新 更多