passport-google-oauth2
passport-google-oauth2 copied to clipboard
userProfileURL is not working
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost/auth/google/secrets" userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo" },
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost/auth/google/secrets", userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo" },
userProfileURL is not working
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" },
You don't need to use "userProfileURL" anymore, as the latest version (v2.0.0) of the npm package "passport-google-oauth20" solves the Google+ account issue which was there couple of years before.
Please refer this article by one of the collaborators for more.
The "userProfileURL" is no longer required, that issue came back in 2018, when google was deprecating it's google + services. This issue has been fixed now with the new npm package
The issue was "Google+ deprecation " so this link was the solutions "userProfileURL:"https://www.googleapis.com/oauth2/v3/userinfo" for that . This issue was fixed we have no longer need this "userProfileURL".
Register or Login button won't load to google auth login page just keep loading at localhost please how do i fix this is my import correct? i'm using ES6
import { Strategy as GoogleStrategy } from "passport-google-oauth20"
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_S, callbackURL: "http://localhost:3000/auth/google/secrets" }, function(accessToken, refreshToken, profile, cb) { User.findOrCreate({ googleId: profile.id }, function (err, user) { console.log(user); return cb(err, user); }); } ));
app.get("/auth/google",function(req,res){ passport.authenticate("google",{ scope: ["profile"] }) })
Even I am facing the same issue. My google auth wont work on both login and register page, but the other functionalities like login and register using mongodb works fine. Someone please help with this issue!!
Reference: Currently doing the Angela Yu's Web Development course where she uses google oauth 2.0 to help in logging in with Google
-
Change app.get('/auth/google") : app.get("/auth/google", passport.authenticate('google',{ scope: ["profile"] }), function(req, res) { // Successful authentication, redirect to secrets. res.redirect("/auth/google/secrets"); });
-
Use : passport.serializeUser((user, done) => done(null, user)); passport.deserializeUser((user, done) => done(null, user)); Instead of: passport.serializeUser(User.serializeUser()); passport.deserializeUser(User.deserializeUser());
-
Also, remove: userProfileURL :"https://www.googleapis.com/oauth2/v3/userinfo" from GoogleStrategy. It is no longer needed. Google Auth popup must work now.
- Change app.get('/auth/google") : app.get("/auth/google", passport.authenticate('google',{ scope: ["profile"] }), function(req, res) { // Successful authentication, redirect to secrets. res.redirect("/auth/google/s
first part what to change? im not still not getting the proper output
You no longer need to use "userProfileURL" because the most recent version (v2.0.0) of the npm package resolves the Google+ account issue that existed a few years ago.
@ShubhamSharma2003 just use /auth/google/secrets in
app.get("/auth/google/secrets", passport.authenticate("google", {failureRedirect: "/login"}), function(req, res) { res.redirect("/secrets"); } )
userProfileURL :"https://www.googleapis.com/oauth2/v3/userinfo" from GoogleStrategy. It is no longer needed.
Register or Login button won't load to google auth login page just keep loading at localhost please how do i fix this is my import correct? i'm using ES6
import { Strategy as GoogleStrategy } from "passport-google-oauth20"
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_S, callbackURL: "http://localhost:3000/auth/google/secrets" }, function(accessToken, refreshToken, profile, cb) { User.findOrCreate({ googleId: profile.id }, function (err, user) { console.log(user); return cb(err, user); }); } ));
app.get("/auth/google", passport.authenticate('google',{ scope: ["profile"] }) );
TokenError: Bad Request
at OAuth2Strategy.parseErrorResponse (C:\Users\User\Desktop\Secrets - Starting Code\node_modules\passport-oauth2\lib\strategy.js:373:12)
at OAuth2Strategy._createOAuthError (C:\Users\User\Desktop\Secrets - Starting Code\node_modules\passport-oauth2\lib\strategy.js:420:16)
at C:\Users\User\Desktop\Secrets - Starting Code\node_modules\passport-oauth2\lib\strategy.js:177:45
at C:\Users\User\Desktop\Secrets - Starting Code\node_modules\oauth\lib\oauth2.js:191:18
at passBackControl (C:\Users\User\Desktop\Secrets - Starting Code\node_modules\oauth\lib\oauth2.js:132:9)
at IncomingMessage.
my code:
const express = require('express');
const bodyparser = require('body-parser'); const ejs = require('ejs'); const mongoose = require('mongoose'); const user = require('./database'); const encrypt = require('mongoose-encryption'); const session = require('express-session'); const passport = require('passport'); const passportlocalmongoose = require('passport-local-mongoose'); const GoogleStrategy = require('passport-google-oauth20').Strategy;
const app = express();
app.set('view engine', 'ejs');
app.use(bodyparser.urlencoded({extended:true})); app.use(express.static('public'));
app.use(session({ secret:'mylittlesecret', resave:false, saveUninitialized:false }));
app.use(passport.initialize()); app.use(passport.session());
mongoose.connect('mongodb://127.0.0.1:27017/UserDB').then(()=>{ console.log('connected to UserDB ...........') }).catch(err => console.log(err));
passport.use(user.createStrategy());
passport.serializeUser((user, done) => done(null, user)); passport.deserializeUser((user, done) => done(null, user));
passport.use(new GoogleStrategy({ clientID: process.env.CLIENT_ID, clientSecret: process.env.CLIENT_SECRET, callbackURL: "http://localhost:3000/auth/google/secrets" }, function(accessToken, refreshToken, profile, cb) { user.findOrCreate({ googleId: profile.id }, function (err, user) { return cb(err, user); }); } ));
app.get('/',(req,res)=>{ res.render('home'); })
//google authentication app.get("/auth/google", passport.authenticate('google',{ scope: ["profile"] }), function(req, res) { // Successful authentication, redirect to secrets. res.redirect("/auth/google/secrets"); });
app.get('/auth/google/secrets', passport.authenticate('google', { failureRedirect: '/login' }), function(req, res) { // Successful authentication, redirect home. res.redirect('/secrets'); });
please I need help
Try replacing : passport.serializeUser((user, done) => done(null, user)); passport.deserializeUser((user, done) => done(null, user));
With: passport.serializeUser(function(user, cb) { process.nextTick(function() { return cb(null, { id: user.id, username: user.username, picture: user.picture }); }); });
passport.deserializeUser(function(user, cb) { process.nextTick(function() { return cb(null, user); }); });