node.jspassport.jspassport-github2

Passport Github strategy not working and throwing errors


The code below is getting mad at me. I don't see anything wrong with it.

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("/");
    }
);

It's throwing a big error every time I try to authenticate. Please help.

Edited the question and did as @jasonandmonte said and now I get this:The I now get after following instructions from an answer.


Solution

  • The issue I am seeing is you are defining a User function that is only logging the profile. The User function in the documentation is an example of a database model that queries a database and passes data to a callback.

    To resolve the issue and replicate how passport-github's example does it, you will need to use an object modeling tool like Mongoose or Sequelize. You will then have access to something similar to User.findOrCreate() or just User.find() if you want to restrict account creation to an admin role.

    To get passport working until you setup a database connection though, you should be able to update the strategy callback to invoke done.

    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);
            }
        )
    );