【问题标题】:Postgraphile + express + jwt is throwing 'No authorization token found' even though graphiql worksPostgraphile + express + jwt 正在抛出“未找到授权令牌”,即使 graphiql 有效
【发布时间】:2019-03-04 04:19:32
【问题描述】:

我正在尝试将 jwt express 中间件和 postgraphile 中间件一起使用,并且我想添加一个例外,以便在 graphiql 界面中使用身份验证令牌。我知道 postgraphile 在 /graphql 上启动 graphql,在 /graphiql 上启动 graphiql,除非另有配置。所以在我的 jwt 中间件初始化中,我正在这样做:

app.use(jwt({ secret: process.env.JWT_SECRET}).unless({path: ['/', /\/graphiql/i, /\/graphql/i, /auth/i]}));

这使得 /graphiql/graphql/auth 路由不需要身份验证令牌。这似乎工作正常,因为我可以很好地进入 graphiql 界面,并且我正在构建的 graphql 查询工作得很好。但是,在我的快递终端中,当我加载 http://localhost:3000/graphiql 时,它仍然试图连接到一个 No authorization token found

对于我还必须添加到 JWT 中间件中的 unless() 方法中的其他内容有什么想法吗?

【问题讨论】:

    标签: node.js express jwt graphql


    【解决方案1】:

    PostGraphile 当前从 /_postgraphile/* 加载 GraphiQL 资产(JS、CSS),因此您可能希望将这些资产列入白名单。

    另一种方法是在添加jwt 中间件之前安装postgraphile - 这样它就不会受到它的影响,并且您不需要维护.unless 列表。

    app.use(postgraphile(...));
    app.use(jwt(...));
    app.use(...);
    

    【讨论】:

      猜你喜欢
      • 2019-12-11
      • 2018-06-05
      • 2020-10-20
      • 2021-04-07
      • 2017-06-12
      • 2023-03-18
      • 2023-03-25
      • 2017-08-22
      • 2016-06-22
      相关资源
      最近更新 更多