【发布时间】:2015-03-23 19:04:10
【问题描述】:
我正在尝试使用 OAuth2、GitHub 对用户进行身份验证,并使用护照来执行此操作。
我有一个非常简单的用户模型,纯粹用于测试目的。
var mongoose = require("mongoose"),
Schema = mongoose.Schema;
UserSchema = new Schema({
email: { type: String, unique: true }
});
mongoose.model("User", UserSchema);
路线或多或少直接来自护照文件。
router.get("/github", passport.authenticate("GitHub", { scope: ["user"] }));
router.get("/github/callback", passport.authenticate("GitHub", {
successRedirect: "/#/user/authenticated",
failureRedirect: "/#/auth/error"
}));
serializeUser 和 deserializeUser 之后,我有以下内容:
passport.use('GitHub', new OAuth2Strategy({
authorizationURL: 'https://github.com/login/oauth/authorize',
tokenURL: 'https://github.com/login/oauth/access_token',
clientID: 'client id',
clientSecret: 'too secret for you to handle',
callbackURL: 'http://dev.corvid.com:3000/auth/github/callback'
}, function(accessToken, refreshToken, profile, done) {
User.findOneAndUpdate(
{ _id: profile.id }, // what to query for
{ $set: { email: profile.email } }, // the update arguments
{ new: true, upsert: true}, // the options
function(err, user) {
done(err, user);
});
}
));
但是,我似乎无法让它按预期工作。它指示我在我接受的 github 上进行身份验证。然后我被重定向回我的站点以获取以下消息:
{"message":"Cast to string failed for value \"undefined\" at path \"email\""}
快速的 console.log 显示配置文件是一个空对象 {}。
我应该如何在单页应用上管理更新插入用户和使用护照对 OAuth2 进行身份验证?
编辑:最后一页的网址如下所示:http://dev.corvid.com:3000/auth/github/callback?code=xxxx
【问题讨论】:
标签: node.js mongodb mongoose passport.js