验证firebase令牌时出错:`TypeError:firebase.auth(...)。verifyIdToken不是函数`

时间:2016-10-01 16:26:14

标签: node.js firebase token firebase-authentication

我正在尝试验证我的nodejs app中前端生成的id令牌。我正在关注this指南。

令牌生成正常。我把它保存为cookie。我可以将它打印到nodejs控制台。当我尝试验证它时,我收到此错误:

TypeError: firebase.auth(...).verifyIdToken is not a function
    at router.get (/home/marcel/code/clairitech/website/routes/index.js:30:21)
    at Layer.handle [as handle_request] (/home/marcel/code/clairitech/website/node_modules/express/lib/router/layer.js:95:5)
    at next (/home/marcel/code/clairitech/website/node_modules/express/lib/router/route.js:131:13)
    at Route.dispatch (/home/marcel/code/clairitech/website/node_modules/express/lib/router/route.js:112:3)
    at Layer.handle [as handle_request] (/home/marcel/code/clairitech/website/node_modules/express/lib/router/layer.js:95:5)
    at /home/marcel/code/clairitech/website/node_modules/express/lib/router/index.js:277:22
    at Function.process_params (/home/marcel/code/clairitech/website/node_modules/express/lib/router/index.js:330:12)
    at next (/home/marcel/code/clairitech/website/node_modules/express/lib/router/index.js:271:10)
    at Function.handle (/home/marcel/code/clairitech/website/node_modules/express/lib/router/index.js:176:3)
    at router (/home/marcel/code/clairitech/website/node_modules/express/lib/router/index.js:46:12)

这是我的代码:

const firebase = require('firebase').initializeApp({
  apiKey: "AIzaSyDLT76jVp0F2_fqbvV8azRz3HybD-****",
  authDomain: "****.firebaseapp.com",
  databaseURL: "https://****.firebaseio.com",
  storageBucket: "****.appspot.com",
  messagingSenderId: "******"
}, 'admin')
router.get('/', (req, res) => {
    firebase.auth().verifyIdToken(req.cookies.firebase_token).then(token => {
      console.log('token:')
      console.log(token)
    }).catch(err => { throw err })
})

我使用的是来自npm和节点版本6.2.2的firebase 3.4.1。

编辑1:添加了firebase配置。

1 个答案:

答案 0 :(得分:0)

也许你只需要引用firebase

var firebase = require('firebase').initializeApp(config);
......