Passport身份验证错误

时间:2016-02-10 15:25:17

标签: node.js express passport-facebook

当我与passport-facebook模块一起执行主应用程序时,我收到以下错误。我再次尝试npm install passport-facebook,但没有任何变化。我需要帮助来解决这个问题。 authenticate()是否是预先定义的护照模块方法?

C:\codebag\app\routes.js:53
app.get('/api/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));
                                       ^

TypeError: Cannot read property 'authenticate' of undefined
    at module.exports (C:\codebag\app\routes.js:53:43)
    at Object.<anonymous> (C:\codebag\server.js:49:24)
    at Module._compile (module.js:413:34)
    at Object.Module._extensions..js (module.js:422:10)
    at Module.load (module.js:357:32)
    at Function.Module._load (module.js:314:12)
    at Function.Module.runMain (module.js:447:10)
    at startup (node.js:139:18)
    at node.js:999:3

routes.js

var express = require('express')
//var router = express.Router();
module.exports = function(app,passport) {
    app.get('*', function(req, res) {
        res.sendfile('./public/index.html');
    });

    // route for facebook authentication and login
    app.get('/api/auth/facebook', passport.authenticate('facebook', { scope : 'email' }));

    // handle the callback after facebook has authenticated the user
    app.get('/api/auth/facebook/callback', passport.authenticate('facebook', {
        successRedirect : '/profile',
        failureRedirect : '/'
    }));

    // route for logging out
    app.get('/api/logout', function(req, res) {
        req.logout();
        res.redirect('/');
    });
};

// route middleware to make sure a user is logged in
function isLoggedIn(req, res, next) {
    // if user is authenticated in the session, carry on
    if (req.isAuthenticated())
        return next();

    // if they aren't redirect them to the home page
    res.redirect('/');
}

server.js

// modules =================================================
var express        = require('express');
var app            = express();
var mongoose       = require('mongoose');
var bodyParser     = require('body-parser');
var methodOverride = require('method-override');
var db = require('./config/db');
var passport = require('passport');
var port = process.env.PORT || 8080; 
mongoose.connect(db.url); 
app.use(bodyParser.json()); 
app.use(bodyParser.json({ type: 'application/vnd.api+json' })); // parse application/vnd.api+json as json
app.use(bodyParser.urlencoded({ extended: true })); // parse application/x-www-form-urlencoded
app.use(passport.initialize());
app.use(passport.session());
app.use(methodOverride('X-HTTP-Method-Override')); // override with the X-HTTP-Method-Override header in the request. simulate DELETE/PUT
app.use(express.static(__dirname + '/public')); // set the static files location /public/img will be /img for users

// routes ==================================================
require('./app/routes')(app); // pass our application into our routes

// start app ===============================================
app.listen(port);   
console.log('Magic happens on port ' + port);           // shoutout to the user
exports = module.exports = app;                         // expose app

2 个答案:

答案 0 :(得分:1)

在server.js文件中,我已经使用“require('。/ app / routes')(app)”将“app”单独传递给routes.js。所以我将此语句编辑为“require('./ app /路由)(应用程序,护照)”。 现在它有效。谢谢你

答案 1 :(得分:0)

您的router.js中不需要护照,没有它,变量为undefined

将其添加到routes.js的开头

var passport = require('passport');