Node.JS 验证 Google 身份验证令牌
Node.JS verify Google Authentication token
我正在尝试使用 Express.JS 框架构建一个 Node.JS REST 服务器,该框架为移动应用程序集成了 Google 身份验证。 Node.JS 使用的版本是 0.12.7.
我在验证从应用程序收到的 Google 令牌时遇到问题,因为我尝试加载的模块 returns 似乎出错了。
用于验证此令牌的模块是passport-google-token。用于初始化该模块和检查token的代码如下:
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
据我所知,调用构造函数时出现问题,但我不确定是什么问题。
你能帮帮我吗?
谢谢。
尝试更改您的导入代码
import GoogleTokenStrategy from 'passport-google-token';
到
import { Strategy as GoogleTokenStrategy } from 'passport-google-token';
我正在尝试使用 Express.JS 框架构建一个 Node.JS REST 服务器,该框架为移动应用程序集成了 Google 身份验证。 Node.JS 使用的版本是 0.12.7.
我在验证从应用程序收到的 Google 令牌时遇到问题,因为我尝试加载的模块 returns 似乎出错了。
用于验证此令牌的模块是passport-google-token。用于初始化该模块和检查token的代码如下:
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
据我所知,调用构造函数时出现问题,但我不确定是什么问题。
你能帮帮我吗?
谢谢。
尝试更改您的导入代码
import GoogleTokenStrategy from 'passport-google-token';
到
import { Strategy as GoogleTokenStrategy } from 'passport-google-token';