Auth0和Graphql错误-TypeError:无法读取未定义的属性'publicKey'

时间:2019-09-06 20:48:14

标签: node.js express graphql auth0 express-graphql

我当前正在尝试通过Auth0为Graphql设置身份验证。我们面临的问题是在我们的身份验证Mutation中返回错误

const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firestore = require('@google-cloud/firestore')
admin.initializeApp();

const db = admin.firestore()

/* for counting documents created */
exports.countDoc = functions.firestore
  .document('collection/{docId}')
  .onCreate((change, context) => {
    const docId = context.params.docId
    db.doc('stats/doc').update({
      'docsCreated': firestore.FieldValue.increment(1)
    })
    return true;
  });

/* for counting users created */
exports.countUsers = functions.auth.user().onCreate((user) => {
  db.doc('stats/doc').update({
    'usersCreated': firestore.FieldValue.increment(1)
  })
  return true;
});

我们已经验证了idToken正在通过validateAndParseIdToken函数进行解码

\validateAndParseIdToken.js:25
      jwt.verify(idToken, key.publicKey, {
                              ^

TypeError: Cannot read property 'publicKey' of undefined
validateAndParseIdToken.js

const jwksClient = require('jwks-rsa')
const jwt = require('jsonwebtoken')
const jwks = jwksClient({
  cache: true,
  rateLimit: true,
  jwksRequestsPerMinute: 1,
  jwksUri: `https://${process.env.AUTH0_DOMAIN}/.well-known/jwks.json`
})

const validateAndParseIdToken = (idToken) => new Promise((resolve, reject) => {
  const { header, payload} = jwt.decode(idToken, {complete: true})
  if (!header || !header.kid || !payload) reject(new Error('Invalid Token'))
  jwks.getSigningKey(header.kid, (err, key) => {
    if (err) reject(new Error('Error getting signing key: ' + err.message))
    jwt.verify(idToken, key.publicKey, { algorithms: ['RS256'] }, (err, decoded) => {
      if (err) reject('jwt verify error: ' + err.message)
      resolve(decoded)
    })
  })
})

module.exports = validateAndParseIdToken

期望流程将是检查idToken,如果有效,则检查数据库中是否存在该用户的用户,否则请创建该用户。

0 个答案:

没有答案