Unknown "Google" authentication strategy

I have been following this tutorial and I keep getting the error "unknown authentication strategy" google. I have no idea what is going on and I have already tried looking for other answers with little success. Any help would be appreciated, thanks!

Mistake:

Unknown authentication strategy "google"
Error: Unknown authentication strategy "google"
  at attempt (D:\Programming\weebly-site\node_modules\passport\lib\middleware\authenticate.js:166:37)
  at authenticate (D:\Programming\weebly-site\node_modules\passport\lib\middleware\authenticate.js:342:7)
  at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\route.js:100:13)
  at Route.dispatch (D:\Programming\weebly-site\node_modules\express\lib\router\route.js:81:3)
  at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:234:24
  at Function.proto.process_params (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:312:12)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:228:12
  at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:191:16
  at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:191:16
  at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
  at Function.proto.handle (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:165:3)
  at router (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:34:12)
  at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
  at trim_prefix (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:270:13)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:237:9
  at Function.proto.process_params (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:312:12)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:228:12
  at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:560:15
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:186:14)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:191:16
  at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
  at Function.proto.handle (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:165:3)
  at router (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:34:12)
  at Layer.handle [as handle_request] (D:\Programming\weebly-site\node_modules\express\lib\router\layer.js:76:5)
  at trim_prefix (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:270:13)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:237:9
  at Function.proto.process_params (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:312:12)
  at D:\Programming\weebly-site\node_modules\express\lib\router\index.js:228:12
  at Function.match_layer (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:295:3)
  at next (D:\Programming\weebly-site\node_modules\express\lib\router\index.js:189:10)
  at SendStream.error (D:\Programming\weebly-site\node_modules\express\node_modules\serve-static\index.js:98:7)
  at SendStream.emit (events.js:95:17)
  at SendStream.error (D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:244:17)
  at SendStream.onStatError (D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:340:48)
  at next (D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:619:28)
  at D:\Programming\weebly-site\node_modules\express\node_modules\send\index.js:627:23
  at Object.oncomplete (fs.js:107:15)

      

admin.js

// Generated by CoffeeScript 1.8.0
(function() {
  var express, isLoggedIn, passport, router;

  express = require('express');

  passport = require('passport');

  router = express.Router();

  isLoggedIn = function(req, res, next) {
    if (req.isAuthenticated()) {
      return next();
    }
    res.redirect("/admin");
  };

  router.get('/', function(req, res) {
    return res.render('admin/admin_index.jade');
  });

  router.get('/editor', isLoggedIn, function(req, res) {
    return res.render('admin/admin_editor.jade');
  });

  router.get('/auth/google', passport.authenticate('google', {
    scope: ['profile', 'email']
  }));

  router.get('/auth/google/callback', passport.authenticate('google', {
    successRedirect: '/editor',
    failureRedirect: '/'
  }));

  module.exports = router;

}).call(this);

      

passport.js

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

var User = require('../models/users.js');

var configAuth = require('./auth.js');

module.exports = function(passport) {   
    passport.use(new GoogleStrategy({
        clientID        : configAuth.googleAuth.clientID,
        clientSecret    : configAuth.googleAuth.clientSecret,
        callbackURL     : configAuth.googleAuth.callbackURL,
    }, 
    passport.serializeUser(function(user, done){
        done(null, user.id);
    });

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

    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.ggoogle.email = profile.emails[0].value;

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

      

+3


source to share


1 answer


It looks like you don't name require('./passport.js')(passport)

(or whatever). you are not setting up a passport. My guess is perhaps further backed up by the fact that if you include this code from passport.js

, you will see SyntaxError: Unexpected token ;

, because by removing parameters and unnecessary things, this is what you define:

passport.use(new GoogleStrategy({}, passport.serializeUser(function(user, done){});

passport.deserializeUser(function(id, done) { });

function(token, refreshToken, profile, done) { });

      



If you are going to follow this tutorial, you should take a look at the first part, which defines the basic application. In admin.js

add require('./passport.js')(passport)

after the linerequire('passport')

+7


source







All Articles