4

下面的代码对我很生气。我看不出有什么问题。

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 所说的那样做了,现在我明白了:我现在按照答案中的说明得到。

4

1 回答 1

1

我看到的问题是您正在定义一个User仅记录配置文件的函数。文档中的User函数是查询数据库并将数据传递给回调的数据库模型示例。

要解决此问题并复制passport-github示例的操作方式,您将需要使用对象建模工具,例如 Mongoose 或 Sequelize。然后,您将可以访问类似于User.findOrCreate()或仅User.find()当您想将帐户创建限制为管理员角色时的访问权限。

但是,要在设置数据库连接之前让护照工作,您应该能够更新策略回调以调用完成。

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);
        }
    )
);
于 2020-12-21T23:45:25.973 回答