我正在使用带有护照的nodejs,到目前为止,我已经成功地允许用户使用以下内容通过google openID登录到我的应用程序:
passport.use( new GoogleStrategy({
returnURL: 'http://localhost:22169/auth/google/return',
realm: 'http://localhost:22169/'
},
function(identifier, profile, done) {
// async verification
process.nextTick( function() {
profile.identifier = identifier;
return done(null, profile);
});
}
));
一旦完成这项工作,我的下一步就是仅获取openID值,然后使用mongoose将其保存到mongodb中。我认为这应该工作:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/test');
var User = mongoose.model('User', { openId: String });
/*
Some other stuff...
*/
passport.use( new GoogleStrategy({
returnURL: 'http://localhost:22169/auth/google/return',
realm: 'http://localhost:22169/'
},
function(identifier, profile, done) {
User.findOrCreate({ openId: identifier }, function(err, user) {
done(err, user);
});
}
));
但是,当我运行该节点并尝试通过Google登录时,出现一条错误消息:
/Users/mporter/Sites/budgeter/b-server.js:42
User.findOrCreate({ openId: identifier }, function(err, user) {
^
TypeError: Object function model(doc, fields, skipId) {
if (!(this instanceof model))
return new model(doc, fields, skipId);
Model.call(this, doc, fields, skipId);
} has no method 'findOrCreate'
at Strategy.app.configure.app.use.express.session.secret [as _verify] (/Users/mporter/Sites/budgeter/b-server.js:42:14)
at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/lib/passport-openid/strategy.js:219:16
at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1063:12
at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1203:12
at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1265:7
at Object.openid.loadAssociation (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:107:5)
at _checkSignatureUsingAssociation (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1231:10)
at _checkSignature (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1221:5)
at _verifyAssertionAgainstProvider (/Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1184:3)
at /Users/mporter/Sites/budgeter/node_modules/passport-google/node_modules/passport-openid/node_modules/openid/openid.js:1155:16
我认为它必须与
User
关键字做一些事情,但是我认为我做得正确并创建了一个合适的猫鼬模型。任何人都想伸出援助之手。 最佳答案
如上所述,Passport在您的模型上需要一个findOrCreate
方法,但是该方法未内置在猫鼬模型中。尝试mongoose findorcreate plugin,这会将findOrCreate方法添加到您指定的模型中。