【问题标题】:Apollo Server with Passport JWT带有 Passport JWT 的 Apollo 服务器
【发布时间】:2019-02-22 09:35:12
【问题描述】:

我正在尝试使用护照 JWT 实现 graphql,但行为不是我所期望的。

graphql 端点不应该通过身份验证关闭,因为一些查询是公开的,我想知道如果用户使用解析器登录,如何让 graphql 打开并在内部过滤?

这是我现在的代码,它可以与 JWT 一起正常工作,但它会关闭 graphql 端点并返回“未授权”,除非我指定令牌。

import express from 'express'
import { ApolloServer } from 'apollo-server-express'

import passport from 'passport'
import passportJWT from 'passport-jwt'

import schema from './schemas'

const { JWT_SECRET } = process.env

const path = '/graphql'

// ...

const users = [
  {
    id: 1,
    name: 'John',
    email: 'john@mail.com',
    password: 'john123'
  },
  {
    id: 2,
    name: 'Sarah',
    email: 'sarah@mail.com',
    password: 'sarah123'
  }
]

// ...

const { Strategy, ExtractJwt } = passportJWT

const params = {
  secretOrKey: JWT_SECRET,
  jwtFromRequest: ExtractJwt.fromAuthHeaderAsBearerToken()
}

// ...

const strategy = new Strategy(params, (payload, done) => {
  const user = users[payload.id] || null

  if (user) {
    return done(null, {
      id: user.id
    })
  }

  return done(new Error('The user has not been found'), null)
})

passport.use(strategy)

// ...

const app = express()

passport.initialize()

app.use(path, passport.authenticate('jwt', { session: false }))

// ...

const server = new ApolloServer({
  schema
})

server.applyMiddleware({
  app,
  path
})

app.listen(
  {
    port: 4000
  },
  () => console.log(`The GraphQL server is running on port ${GRAPHQL_PORT}`)
)

【问题讨论】:

    标签: node.js express jwt graphql apollo-server


    【解决方案1】:

    如果 JWTToken 不存在,passport-jwt 将失败,未授权状态传递给 info。你可以像下面这样处理它们

    app.use(path, bodyParser.json(), function(req, res, next) {
        passport.authenticate('jwt', (err, user, info) => {
            if (err) { res.status(500).send({"error": err}); return; }
            next();
        })(req, res, next);
    });
    

    【讨论】:

      猜你喜欢
      • 2016-10-23
      • 2019-06-25
      • 2019-01-27
      • 2019-03-29
      • 2020-03-21
      • 2018-01-08
      • 2020-11-10
      • 2020-05-31
      • 2018-02-19
      相关资源
      最近更新 更多