node.jsexpressgoogle-oauthpassport.jspassport-google-oauth

Getting Missing required parameter: scope Passport.js, express


Im using Passport.js with express in my app to login with Google Oauth. But when i try to sign in, i get the following error: invalid parameter value for redirect_uri: Missing authority: http:localhost:3000/google/callback from which when i access localhost:3000/google/callback, i get Missing required parameter: scope. The relevant code:

const express = require("express");
const cors = require("cors");
const mongoose = require("mongoose");
const passport = require("passport");
const app = express();
const port = process.env.PORT || 3000;
require("dotenv").config();
require("./passport-setup")

app.use(passport.initialize())
app.use(passport.session())

app.get('/success', (req, res) => {
  res.render("/profile.html")
})

app.get('/login', passport.authenticate('google', { scope: 'email' }));
app.get('/google/callback', passport.authenticate('google', { failureRedirect: '/failed' }),
  function(req, res) {
    // Successful authentication, redirect home.
    res.redirect('/success');
  }
);

passport config(relevant code):

const passport = require("passport");

const GoogleStrategy = require("passport-google-oauth2").Strategy

passport.use(new GoogleStrategy({
    clientID: process.env.GOOGLE_CLIENT_ID,
    clientSecret: process.env.GOOGLE_CLIENT_SECRET,
    callbackURL: process.env.CALLBACK_URL,
    passReqToCallback: true
},function(request,accessToken,refreshToken,profile,done){
    console.log(profile)
    return done(null, profile)
}

))

PS: I found this answer but i don't know what he means by 'JSON key'. Maybe the API updated.

Any help would be really appreciated. Thanks in advance.


EDIT

The callback url I provided to google was not matching my `app.get`. Fixed.

Solution

  • My callback URL was http:localhost:3000/google/callback

    Setting it to /gooogle/callback worked for me.