Google Plus Authentication Callback Not working

问题内容:

I’ve added Google + sign in but the callback doesn’t seem to be working.

Note: The code was adapted from this tutorial: https://scotch.io/tutorials/easy-node-authentication-google

The Scenario

  1. I go to the URL: https://gym-up-server.herokuapp.com/api/v1/oauth/google
  2. This initiates the Google Login Process through their website
  3. Login is successful + Data is inserted into MySQL DB
  4. App then gets taken to Error Page

It seems to be failing to call the callback URL which should be:
https://gym-up-server.herokuapp.com/api/v1/oauth/google/callback

I’m using the following dependencies: Express, MySQL, Sequelize, Passport

Here is my code:

Routes:

// =====================================
// GOOGLE ROUTES =======================
// =====================================
// send to google to do the authentication
// profile gets us their basic information including their name
// email gets their emails
router.get(
  "/google",
  passport.authenticate("google", { scope: ["profile", "email"] })
);

// the callback after google has authenticated the user
router.get("/google/callback", passport.authenticate("google"), 
function(req,res) {
  res.render("index");
  console.log("After Passport AUTH");
});

Passport JS

// =========================================================================    


// GOOGLE ==================================================================
  // =========================================================================
  passport.use(
    new GoogleStrategy(
      {
        clientID: configAuth.googleAuth.clientID,
        clientSecret: configAuth.googleAuth.clientSecret,
        callbackURL: configAuth.googleAuth.callbackURL
      },
      function(token, refreshToken, profile, done) {
        console.log("getting data from Google");
        // make the code asynchronous
        // User.findOne won't fire until we have all our data back from Google
        process.nextTick(function() {
          // try to find the user based on their google id
          console.log("profile ID :", profile.id);
          models.User.findOne({ where: { ggId: profile.id } }).then(function(
            user
          ) {
            // if (err) return done(err);
            if (user) {
              // if a user is found, log them in
              return done(null, user);
            } else {
              // if the user isnt in our database, create a new user
              var data = {
                // set all of the relevant information
                ggId: profile.id,
                ggToken: token,
                ggName: profile.displayName,
                ggEmail: profile.emails[0].value // pull the first email
              };
              //save user
              models.User.create(data, {
                fields: ["ggId", "ggToken", "ggName", "ggEmail"]
              }).then(function(insertedUser) {
                console.log(
                  "User Created!" + ": " + insertedUser.get({ plain: true })
                );
                // console.log("about to run DONE to go back to ROUTE");
                // return done(null, insertedUser.get({ plain: true }));
                if (!insertedUser) {
                  console.log("failed to insert user - nothing found!");
                  return done(null, false);
                }
                if (insertedUser) {
                  console.log("about to run DONE to go back to ROUTE");
                  return done(null, insertedUser);
                }
              });
            }
          });
        });
      }
    )
  );

问题评论:

原文地址:

https://stackoverflow.com/questions/47753034/google-plus-authentication-callback-not-working

添加评论

友情链接:蝴蝶教程