1

我正在嘗試使用Express.JS框架構建一個Node.JS REST服務器,該框架集成了Google身份驗證的移動應用程序。使用的Node.JS版本是0.12.7。Node.JS驗證Google身份驗證令牌

我在驗證從應用程序接收到的Google令牌時遇到了問題,因爲似乎我正在嘗試加載的模塊會返回錯誤。

用於驗證此令牌的模塊是passport-google-token [1]。使用的代碼初始化該模塊並檢查令牌如下:

index.js文件

'use strict'; 

import express from 'express'; 
import passport from 'passport'; 
import {setTokenCookie} from '../../auth.service'; 

var router = express.Router(); 

router 
    .post('/callback', passport.authenticate('google-token'), setTokenCookie); 

export default router; 

passport.js文件

import passport from 'passport'; 
import GoogleTokenStrategy from 'passport-google-token'; 

export function setup(User, config) { 
    passport.use(new GoogleTokenStrategy({ 
    clientID: config.google.clientID, 
    clientSecret: config.google.clientSecret 
    }, 
    function(accessToken, refreshToken, profile, done) { 
    User.findOne({'google.id': profile.id}).exec() 
     .then(user => { 
     if (user) { 
      console.log(user); 
      return done(null, user); 
     } 

     user = new User({ 
      name: profile.displayName, 
      email: profile.emails[0].value, 
      role: 'user', 
      username: profile.emails[0].value.split('@')[0], 
      provider: 'google', 
      google: profile._json 
     }); 
     console.log(user); 
     user.save() 
      .then(user => done(null, user)) 
      .catch(err => done(err)); 
     }) 
     .catch(err => done(err)); 
    })); 
} 

當我嘗試啓動我的服務器收到以下錯誤:

D:\Work\SoftwareUp\softwareup_android_demo\server\server\auth\google\mobile\passport.js:19 
    _passport2.default.use(new _passportGoogleToken2.default({ 
         ^
TypeError: object is not a function 
    at Object.setup (D:/Work/SoftwareUp/softwareup_android_demo/server/server/auth/google/mobile/passport.js:5:16) 
    at Object.<anonymous> (D:/Work/SoftwareUp/softwareup_android_demo/server/server/auth/index.js:13:37) 
    at Module._compile (module.js:460:26) 
    at loader (D:\Work\SoftwareUp\softwareup_android_demo\server\node_modules\babel-register\lib\node.js:144:5) 
    at Object.require.extensions.(anonymous function) [as .js] (D:\Work\SoftwareUp\softwareup_android_demo\server\node_modules\babel-register\lib\node.js:154:7) 
    at Module.load (module.js:355:32) 
    at Function.Module._load (module.js:310:12) 
    at Module.require (module.js:365:17) 
    at require (module.js:384:17) 
    at Object.exports.default (D:/Work/SoftwareUp/softwareup_android_demo/server/server/routes.js:15:20) 
    at Object.<anonymous> (D:/Work/SoftwareUp/softwareup_android_demo/server/server/app.js:27:1) 
    at Module._compile (module.js:460:26) 
    at loader (D:\Work\SoftwareUp\softwareup_android_demo\server\node_modules\babel-register\lib\node.js:144:5) 
    at Object.require.extensions.(anonymous function) [as .js] (D:\Work\SoftwareUp\softwareup_android_demo\server\node_modules\babel-register\lib\node.js:154:7) 
    at Module.load (module.js:355:32) 
    at Function.Module._load (module.js:310:12) 
    at Module.require (module.js:365:17) 
    at require (module.js:384:17) 
    at Object.<anonymous> (D:\Work\SoftwareUp\softwareup_android_demo\server\server\index.js:12:28) 
    at Module._compile (module.js:460:26) 
    at Object.Module._extensions..js (module.js:478:10) 
    at Module.load (module.js:355:32) 
Stopping Express server 

從我認爲有一個問題時調用c onstructor,但我不知道是什麼問題。

你能幫我嗎?

謝謝。

[1] https://www.npmjs.com/package/passport-google-token

回答

2

試圖改變自己的進口代碼

import GoogleTokenStrategy from 'passport-google-token';

import { Strategy as GoogleTokenStrategy } from 'passport-google-token';

+0

謝謝,這解決了我的問題,它似乎工作現在。 – Ancuta