1

I have tried nearly everything (read threads,searched on google) but i don't understand why is my callback url not being executed.After debugging, i found that the request is being sent to google servers, i get back the profile.But the callback is not being executed.The page just hangs there as soon as i navigate to http:\localhost:3000\auth\google.I am using a localhost server currently. Although the data is being persisted on mongodb which means that the route is working but not the callback.

Below are my routes and local strategy.

AuthRoutes.js

const passport = require("passport");
module.exports = app => {
  app.get("/auth/google/", (req, res) => {
    passport.authenticate("google", {

      scope: ["profile", "email"] 
    });
  });

  app.get(
    "/auth/google/callback",
    passport.authenticate("google", {
      successRedirect: "/profile",
      failureRedirect: "/"
    })
  );

  app.get("/api/current_user", (req, res) => {
    res.send(req.user);

  });

  app.get("/api/logout", (req, res) => {
    req.logout();
    res.redirect("/completed"); 
  });
};

passport.js

const passport = require("passport");
const GoogleStrategy = require("passport-google-oauth20").Strategy;
const keys = require("../config/keys");
const mongoose = require("mongoose");

const User = mongoose.model("users");

passport.serializeUser((user, done) => {
  done(null, user.id); 
});
passport.deserializeUser((id, done) => {
  User.findById(id).then(userModel => {
    done(null, userModel);
  });
});

passport.use(
  new GoogleStrategy(
    {
      clientID: keys.GOOGLE_CLIENT_ID,
      clientSecret: keys.GOOGLE_CLIENT_SECRET,
      callbackURL: "/auth/google/callback",
      proxy: true
    },
    (accessToken, refreshToken, profile, cb) => {
      User.findOne({ googleId: profile.id }).then(existinguser => {
        if (existinguser) {
          //done(null, existinguser);
        } else {
          new User({ googleId: profile.id }).save().then(user => {
            //  done(null, user);
          });
        }
      });
    }
  )
);

Feel free to ask anything that i have missed. Thanks.

4

0 回答 0