4

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.

Dingus45191
  • 532
  • 1
  • 7
  • 19
  • I haven't worked with either of these libs but usually, with OAuth, you need to exchange the auth code that retuned on the callback to an access token and refresh token. Have you done this anywhere? – Prav Dec 20 '20 at 15:54
  • well i know that this part is throwing error function(accessToken, refreshToken, profile, done) { User(profile),function (err, user) { return done(err, user); }; – Dingus45191 Dec 20 '20 at 16:07
  • just don't know why – Dingus45191 Dec 20 '20 at 16:07
  • 1
    One thing I noticed on the documentation that it uses `User.findOrCreate()` instead of `User()` – Prav Dec 20 '20 at 16:17
  • when i was using ```User.findOrCreate ```it was saying User is not defined i don't know why it's saying that – Dingus45191 Dec 20 '20 at 16:25
  • so I did some modifications but it's still throwing hell lot of errors. – Dingus45191 Dec 20 '20 at 16:25
  • Can you try to replicate their "working example" here? https://github.com/cfsghost/passport-github/blob/master/examples/login/app.js – Prav Dec 20 '20 at 16:32
  • What's wrong with my code? Need help ASAP! That error is killing me – Dingus45191 Dec 21 '20 at 08:56

1 Answers1

1

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);
        }
    )
);
jasonandmonte
  • 1,869
  • 2
  • 15
  • 24
  • thanks @jasonandmonte. did as you said and now i get a new error. – Dingus45191 Dec 22 '20 at 05:27
  • 1
    What does your express app file look like? You need to use `passport.initialize()` middleware in your express app. If that sounds unfamiliar I would recommend reviewing a passport oauth 2.0 tutorial or [example code](https://github.com/passport/express-4.x-facebook-example/blob/master/server.js) to look at a more comprehensive setup for passport. – jasonandmonte Dec 22 '20 at 05:39