Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

passport.js throws Unknown authentication strategy "Google"

I've been following this guide and I keep getting the error "unknown authentication strategy 'google'". I've no idea what's going on and I already tried searching the other answers with little success. Any help would be appreciated, thanks!

Error:

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);
                    });
                }
            });
        });
    }));
};
like image 912
Andrew Avatar asked Oct 21 '14 03:10

Andrew


1 Answers

It looks to me you are not calling require('./passport.js')(passport) (or whatever the path is) at all ie. you are not configuring passport. My assumption is maybe further backed up by the fact that if you would include that code from passport.js you would see SyntaxError: Unexpected token ; because, removing parameters and excess stuff, 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're gonna follow that tutorial you should have taken a look at the first part that defines the base application. In your admin.js, add require('./passport.js')(passport) after the line require('passport')

like image 116
vesse Avatar answered Sep 20 '22 22:09

vesse