MasterMind
MasterMind

Reputation: 1023

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.

Upvotes: 0

Views: 1781

Answers (2)

Denis Schiopu
Denis Schiopu

Reputation: 11

PS: I found this answer but i don't know what he means by 'JSON key' - the person referred to your GoogleStrategy which is A JSON object contains zero, one, or more key-value pairs.

/* SECURE COOKIES TRUE MADE GOOGLE SIGN IN / SIGNUP WORK */

I was getting an error when trying to Sign in/up with Google account: Missing required parameter: scope and the following lines of code added before app.use(passport.initialize()); made it work!

if (app.get("env") === "production") {
    app.set("trust proxy", 1);
    session.cookie.secure = true;
}

You might have to implement express-session to make this work (check the npm website for docs on how to use) but simply declare at the top:

      const session = require("express-session");

before app.use(passport.initialize()) add this lines:

      app.use(
         session({
            secret: process.env.SESSION_SECRET,
            resave: false,
            saveUninitialized: false,
            cookie: {},
         })
      );

Upvotes: 1

MasterMind
MasterMind

Reputation: 1023

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

Setting it to /gooogle/callback worked for me.

Upvotes: 0

Related Questions