passport.js 抛出未知的身份验证策略“谷歌”
Posted
技术标签:
【中文标题】passport.js 抛出未知的身份验证策略“谷歌”【英文标题】:passport.js throws Unknown authentication strategy "Google" 【发布时间】:2014-12-16 04:35:33 【问题描述】:我一直在关注 this 指南,但一直收到错误消息“未知身份验证策略 'google'”。我不知道发生了什么,我已经尝试搜索其他答案但收效甚微。任何帮助将不胜感激,谢谢!
错误:
Unknown authentication strategy "google"
Error: Unknown authentication strategy "google"
at attempt (D:\Programming\weebly-site\node_modules\passport\lib\middleware\authenticate.js:166:37)
at authenticate (D:\Programming\weebly-site\node_modules\passport\lib\middleware\authenticate.js:342:7)
at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\route.js:100:13)
at Route.dispatch (D:\Programming\weebly-site\node_modules\express\lib\router\route.js:81:3)
at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:234:24
at Function.proto.process_params (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:312:12)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:228:12
at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:191:16
at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:191:16
at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
at Function.proto.handle (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:165:3)
at router (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:34:12)
at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
at trim_prefix (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:270:13)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:237:9
at Function.proto.process_params (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:312:12)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:228:12
at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:560:15
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:186:14)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:191:16
at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
at Function.proto.handle (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:165:3)
at router (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:34:12)
at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
at trim_prefix (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:270:13)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:237:9
at Function.proto.process_params (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:312:12)
at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:228:12
at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
at SendStream.error (D:\Programming\weebly-site\node_modules\express\node_modules\serve-static\index.js:98:7)
at SendStream.emit (events.js:95:17)
at SendStream.error (D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:244:17)
at SendStream.onStatError (D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:340:48)
at next (D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:619:28)
at D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:627:23
at Object.oncomplete (fs.js:107:15)
admin.js
// Generated by CoffeeScript 1.8.0
(function()
var express, isLoggedIn, passport, router;
express = require('express');
passport = require('passport');
router = express.Router();
isLoggedIn = function(req, res, next)
if (req.isAuthenticated())
return next();
res.redirect("/admin");
;
router.get('/', function(req, res)
return res.render('admin/admin_index.jade');
);
router.get('/editor', isLoggedIn, function(req, res)
return res.render('admin/admin_editor.jade');
);
router.get('/auth/google', passport.authenticate('google',
scope: ['profile', 'email']
));
router.get('/auth/google/callback', passport.authenticate('google',
successRedirect: '/editor',
failureRedirect: '/'
));
module.exports = router;
).call(this);
passport.js
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var User = require('../models/users.js');
var configAuth = require('./auth.js');
module.exports = function(passport)
passport.use(new GoogleStrategy(
clientID : configAuth.googleAuth.clientID,
clientSecret : configAuth.googleAuth.clientSecret,
callbackURL : configAuth.googleAuth.callbackURL,
,
passport.serializeUser(function(user, done)
done(null, user.id);
);
passport.deserializeUser(function(id, done)
User.findById(id, function(err, user)
done(err, user);
);
);
function(token, refreshToken, profile, done)
process.nextTick(function()
User.findOne( 'google.id' : profile.id, function(err, user)
if(err)
return done(err);
if(user)
return done(null, user);
else
var newUser = new User();
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.google.name = profile.displayName;
newUser.ggoogle.email = profile.emails[0].value;
newUser.save(function(err)
if(err)
throw err;
return done(null, newUser);
);
);
);
));
;
【问题讨论】:
【参考方案1】:在我看来,您根本没有调用require('./passport.js')(passport)
(或任何路径),即。您没有配置护照。我的假设可能得到以下事实的进一步支持:如果您包含来自 passport.js
的代码,您会看到 SyntaxError: Unexpected token ;
,因为删除参数和多余的东西,这就是您定义的:
passport.use(new GoogleStrategy(, passport.serializeUser(function(user, done));
passport.deserializeUser(function(id, done) );
function(token, refreshToken, profile, done) );
如果您要学习该教程,您应该查看定义基本应用程序的第一部分。在您的admin.js
中,在require('passport')
行之后添加require('./passport.js')(passport)
【讨论】:
类型错误:护照不是函数【参考方案2】:步骤 1- 在 passport.js 中包含护照包并删除 module.exports 并像在 admin.js 中编写代码一样编写代码
Step 2 - 在 admin.js 中包含这一行
require('./passport.js');
包含所有软件包之后。
您的 passport.js 将如下所示 -
var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
var User = require('../models/users.js');
var configAuth = require('./auth.js');
var passport = require('passport');
passport.use(new GoogleStrategy(
clientID : configAuth.googleAuth.clientID,
clientSecret : configAuth.googleAuth.clientSecret,
callbackURL : configAuth.googleAuth.callbackURL,
,
passport.serializeUser(function(user, done)
done(null, user.id);
);
passport.deserializeUser(function(id, done)
User.findById(id, function(err, user)
done(err, user);
);
);
function(token, refreshToken, profile, done)
process.nextTick(function()
User.findOne( 'google.id' : profile.id, function(err, user)
if(err)
return done(err);
if(user)
return done(null, user);
else
var newUser = new User();
newUser.google.id = profile.id;
newUser.google.token = token;
newUser.google.name = profile.displayName;
newUser.ggoogle.email = profile.emails[0].value;
newUser.save(function(err)
if(err)
throw err;
return done(null, newUser);
);
);
);
));
您的 admin.js 将如下所示 -
(function()
var express, isLoggedIn, passport, router;
express = require('express');
passport = require('passport');
**// include the your passport.js file path
require('./passport.js');**
router = express.Router();
isLoggedIn = function(req, res, next)
if (req.isAuthenticated())
return next();
res.redirect("/admin");
;
router.get('/', function(req, res)
return res.render('admin/admin_index.jade');
);
router.get('/editor', isLoggedIn, function(req, res)
return res.render('admin/admin_editor.jade');
);
router.get('/auth/google', passport.authenticate('google',
scope: ['profile', 'email']
));
router.get('/auth/google/callback', passport.authenticate('google',
successRedirect: '/editor',
failureRedirect: '/'
));
module.exports = router;
).call(this);
【讨论】:
以上是关于passport.js 抛出未知的身份验证策略“谷歌”的主要内容,如果未能解决你的问题,请参考以下文章
Passport.js Google 策略不适用于 React 应用程序
Express Sequelize 和 Passport.js 身份验证策略不起作用
带有 Passport 的 Node.js 身份验证:如果缺少字段,如何闪烁消息?