passport / discuss

A forum for discussing topics related to the usage of Passport.js.
1 stars 0 forks source link

GoogleOAuth2.0- Bad Request #43

Open ishandeveloper opened 4 years ago

ishandeveloper commented 4 years ago

Hi, I'm quite new to using this package. I am trying implement OAuth sign-in with google using passport-google-oauth20.

But whenever I try to to sign in, It just sends a 'Bad Request' response to to my browser and I am not getting errors in the terminal.

Here's what I've implemented in index.js

const userSchema = new mongoose.Schema({
    email: String,
    password: String,
    googleId: String,
    secret: String
});

userSchema.plugin(passportLocalMongoose);
userSchema.plugin(findOrCreate);

const User = new mongoose.model("User", userSchema);

passport.use(User.createStrategy());

passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    User.findById(id, function (err, user) {
        done(err, user);
    });
});

passport.use(new GoogleStrategy({
        clientID: process.env.CLIENT_ID,
        clientSecret: process.env.CLIENT_SECRET,
        callbackURL: "http://localhost:8080/auth/google/oauth",
        userProfileURL: "https://www.googleapis.com/oauth2/v3/userinfo"
    },
    function (accessToken, refreshToken, profile, cb) {
        //console.log(profile);

        User.findOrCreate({
            googleId: profile.id
        }, function (err, user) {
            return cb(err, user);
        });
    }
));

app.get("/auth/google/oauth",
    passport.authenticate('google', {
        failureRedirect: "/login"
    }),
    function (req, res) {
        // Successful authentication, redirect to profile page.
        res.redirect("/profile");
    });

It's been two days since I'm trying to figure out a solution on Github, Stackoverflow etc. but nothing seems to have worked out so far.