Node无法调用未定义的方法'serializeUser'

时间:2014-10-21 16:07:20

标签: javascript node.js passport.js

我正在使用Passport与Google创建身份验证系统。我正在关注this教程。但是,在我的passport.js文件中,我一直收到此错误:

D:\Programming\weebly-site\config\passport.js:11
        passport.serializeUser(function(user, done){
                 ^
TypeError: Cannot call method 'serializeUser' of undefined
  at module.exports (D:\Programming\weebly-site\config\passport.js:11:11)
  at Object.<anonymous> (D:\Programming\weebly-site\routes\admin.js:9:46)
  at Object.<anonymous> (D:\Programming\weebly-site\routes\admin.js:39:4)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (D:\Programming\weebly-site\app.js:18:19)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Module.require (module.js:364:17)
  at require (module.js:380:17)
  at Object.<anonymous> (D:\Programming\weebly-site\bin\www:3:11)
  at Module._compile (module.js:456:26)
  at Object.Module._extensions..js (module.js:474:10)
  at Module.load (module.js:356:32)
  at Function.Module._load (module.js:312:12)
  at Function.Module.runMain (module.js:497:10)
  at startup (node.js:119:16)
  at node.js:906:3

这是我的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.serializeUser(function(user, done){
        done(null, user.id);
    });

    passport.deserializeUser(function(id, done) {
        User.findById(id, function(err, user) {
            done(err, user);
        });
    });
    passport.use(new GoogleStrategy({
        clientID        : configAuth.googleAuth.clientID,
        clientSecret    : configAuth.googleAuth.clientSecret,
        callbackURL     : configAuth.googleAuth.callbackURL,
    }, 
    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);
                    });
                }
            });
        });
    }));
};

App.js文件:

var express = require('express');
var path = require('path');
var favicon = require('serve-favicon');
var logger = require('morgan');
var cookieParser = require('cookie-parser');
var bodyParser = require('body-parser');
var coffeeScript = require('coffee-script');
var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');

var configDB = require('./config/database.js');
mongoose.connect(configDB.url);
require('./config/passport.js')(passport);

var routes = require('./routes/index');
var adminRoutes = require('./routes/admin')
var users = require('./routes/users');

var app = express();


app.use('/', routes);
app.use('/admin',adminRoutes);
app.use('/users', users);

 ....
app.use(session({secret: 'lotsofwords', saveUninitialized: true, resave: true}));
app.use(passport.initialize());
app.use(passport.session());
....

module.exports = app;

Admin.js

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

  express = require('express');

  passport = require('passport');

  require('../config/passport.js')(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', 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);

我不知道如何解决这个问题 - 我在SO上搜索过,谷歌无济于事。任何帮助将不胜感激,谢谢!

2 个答案:

答案 0 :(得分:1)

您正在查找错误的文件。错误发生在D:\Programming\weebly-site\routes\admin.js:9:46。您正在调用config/passport.js导出的函数而不传入护照模块。

答案 1 :(得分:0)

你在哪里放置了require语句?      要求( './配置/护照')(的护照);

注意大胆的护照。

你是否在它上面或下面建立了护照变量?

这有效:

  1. var passport = require('passport');
  2. 要求( './配置/护照')(护照); //通过护照进行配置
  3. 其他(失败):

    1. 要求( './配置/护照')(护照); //通过护照进行配置
    2. var passport = require('passport');