Trying to add Google Login to the Website, I have created Project on Google Developers Console, Created the OAuth Credential
and added the Client_ID and Client_Secret to.env file.
Code below :
passport.use(
new GoogleStrategy(
{
clientID: process.env.CLIENT_ID,
clientSecret: process.env.CLIENT_SECRET,
callbackURL: "http://localhost:3000/auth/google/secrets",
userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo",
},
function (accessToken, refreshToken, profile, cb) {
User.findOrCreate({ googleId: profile.id }, function (err, user) {
return cb(err, user);
});
}
)
);
I have also added the plugin for findorCreate:
const findOrCreate = require("mongoose-findorcreate");
userSchema.plugin(findOrCreate);
callback URL is also proper:
And finally this my get method.
app.get("/auth/google", function (req, res) {
passport.authenticate("google", { scope: ["profile"] });
});
question from:
https://stackoverflow.com/questions/65868769/passport-google-auth-is-not-working-with-js 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…