Passport Github 策略无效并抛出错误
Passport Github strategy not working and throwing errors
下面的代码让我很生气。我看不出有什么问题。
function User(profile){
console.log(profile)
}
passport.use(
new GitHubStrategy({
clientID: "my_id",
clientSecret: "secret",
callbackURL: "http://localhost:3000/auth/github/callback",
},
function(accessToken, refreshToken, profile, done) {
User(profile),function (err, user) {
return done(err, user);
};
}
)
);
app.get(
"/auth/github",
passport.authenticate("github", { scope: ["user:email"] })
);
app.get(
"/auth/github/callback",
passport.authenticate("github", { failureRedirect: "/login" }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect("/");
}
);
每次我尝试验证时都会抛出一个大错误。请帮忙。
编辑了问题并按照@jasonandmonte 所说的做了,现在我明白了:
我看到的问题是您正在定义一个只记录配置文件的 User
函数。文档中的 User
函数是查询数据库并将数据传递给回调的数据库模型示例。
要解决此问题并复制 passport-github
的示例的执行方式,您将需要使用对象建模工具,如 Mongoose 或 Sequelize。如果您想将帐户创建限制为管理员角色,您将可以访问类似于 User.findOrCreate()
或 User.find()
的内容。
要在设置数据库连接之前使 passport 正常工作,您应该能够更新策略回调以调用完成。
passport.use(
new GitHubStrategy({
clientID: "my_id",
clientSecret: "secret",
callbackURL: "http://localhost:3000/auth/github/callback",
},
function(accessToken, refreshToken, profile, done) {
done(null, profile.id);
}
)
);
下面的代码让我很生气。我看不出有什么问题。
function User(profile){
console.log(profile)
}
passport.use(
new GitHubStrategy({
clientID: "my_id",
clientSecret: "secret",
callbackURL: "http://localhost:3000/auth/github/callback",
},
function(accessToken, refreshToken, profile, done) {
User(profile),function (err, user) {
return done(err, user);
};
}
)
);
app.get(
"/auth/github",
passport.authenticate("github", { scope: ["user:email"] })
);
app.get(
"/auth/github/callback",
passport.authenticate("github", { failureRedirect: "/login" }),
function(req, res) {
// Successful authentication, redirect home.
res.redirect("/");
}
);
每次我尝试验证时都会抛出一个大错误。请帮忙。
编辑了问题并按照@jasonandmonte 所说的做了,现在我明白了:
我看到的问题是您正在定义一个只记录配置文件的 User
函数。文档中的 User
函数是查询数据库并将数据传递给回调的数据库模型示例。
要解决此问题并复制 passport-github
的示例的执行方式,您将需要使用对象建模工具,如 Mongoose 或 Sequelize。如果您想将帐户创建限制为管理员角色,您将可以访问类似于 User.findOrCreate()
或 User.find()
的内容。
要在设置数据库连接之前使 passport 正常工作,您应该能够更新策略回调以调用完成。
passport.use(
new GitHubStrategy({
clientID: "my_id",
clientSecret: "secret",
callbackURL: "http://localhost:3000/auth/github/callback",
},
function(accessToken, refreshToken, profile, done) {
done(null, profile.id);
}
)
);