node.js Googe Authentication callback error

Viewed 80

I'm trying to set up Googe Authentication to a website running off node.js, passport.js, express & mongodb, largely based off this example. I have managed to get local authentication working but when trying to get Google Authentication working, I'm getting a TypeError: OAuth2Strategy requires a verify callback error when trying to initialise the server.

The authentication strategy for Google is held in /config/passport.js, shown below:

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy();
var User = require('../models/user');
var configAuth = require('./auth');

module.exports = function(passport) {
    // serialize the user session
    passport.serializeUser(function(user, done) {
        done(null, user.id);
    });

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

// =================GOOGLE=======================

    var strategyOptions = {
        clientID        : configAuth.googleAuth.clientID,
        clientSecret    : configAuth.googleAuth.clientSecret,
        callbackURL     : configAuth.googleAuth.callbackURL,

    };
    var verifyCallback = function(token, refreshToken, profile, done) {

        process.nextTick(function() {
            User.findOne({ 'google.id' : profile.id }, function(err, user) {
                if (err)
                    return done(err);

                if (user) {
                    return done(null, user);
                } else {
                    var newUser = new User();

                    newUser.google.id = profile.id;
                    newUser.google.token = token;
                    newUser.google.name = profile.displayName;
                    newUser.google.email = profile.emails[0].value;

                    newUser.save(function(err) {
                        if (err)
                            throw err;
                        return done(null, newUser);
                    });
                }
            });
        });

    };

    passport.use(new GoogleStrategy(strategyOptions, verifyCallback));

};    

My app.js looks as follows (with the invocation of passport.js bolded):

// set up ================================================================
var express = require('express');
var app = express()
var path = require('path');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');

var morgan = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var session = require('express-session');

var configDB = require('./config/database.js');

// config ================================================================

mongoose.connect(configDB.url);

require('./config/passport')(passport);

//set up express application
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser());

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'pug');

app.use(session({ secret: 'what a long string' }));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use(express.static(path.join(__dirname, 'public')));

// routes ================================================================

require('./routes/index.js')(app, passport);

// launch ================================================================

//app.listen(port);
module.exports = app;

I can't tell what is going wrong here, because as far as I can see verifyCallback is being supplied in the passport.use() function. I've spent the last day Googling around and searching through StackOverflow but haven't found a fix that works yet.

Apologies if this is a really obvious error, I'm new to the whole node ecosystem and web design in general. Happy to provide more information if that is helpful.

1 Answers

You have done it well.The issue is with getting google strategy.

Change this line

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy();

To below mentioned one

var GoogleStrategy = require('passport-google-oauth').OAuth2Strategy;
  • You can see the working example here for any other issue.

For Reference: https://github.com/Khushbu-2112/OAuth-google-example

Related