如何在快速负载的路线上获取护照变量

时间:2016-03-23 22:54:02

标签: node.js express passport.js

我试图在我的应用程序中使用护照本地策略,但是在index.js路由上未定义护照变量。以下是来自app.js,passport.js和我的index.js路线的代码。

这是我的app.js:

var express = require("express");
var path = require('path');
var load = require("express-load");
var bodyParser = require("body-parser");
var cookieParser = require("cookie-parser");
var expressSession = require("express-session");
var methodOverride = require('method-override');
var passport = require('passport');
var error = require('./middlewares/error');
var app = express();
var server = require('http').Server(app);
global.mongoose = require('mongoose');
mongoose.connect('mongodb://localhost:27017/test');

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

app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
app.use(cookieParser('talkieparty'));
app.use(expressSession({
    resave: false,
    saveUninitialized: false,
    secret: "149194"
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(methodOverride('_method'));

app.use(passport.initialize());
app.use(passport.session());

app.use(express.static(path.join(__dirname, 'public')));

load('models').then('controllers').then('routes').into(app, passport);

app.use(error.notFound);
app.use(error.serverError);

var User = app.models.user;
var moment = require('moment');
process.on('message', function(data) {//Executa um função ordenada pelo master.
    console.log(process.pid + " executar: " + JSON.stringify(data));
    var action = data.action;
    switch(action) {
        case "confirmation-email-sended":
            var email = data.data.email;
            User.findOne({email: email}, function(error, user) {
                if(error) {
                    console.log(error);
                }else {
                    if(!user) {
                        return console.log("No user was found with e-mail like " + email);
                    }
                    user.register.notified = true;
                    user.register.notified_date = new Date(moment().format());
                    user.save(function(error, user) {
                        if(error) {
                            console.log(error);
                        }else {
                            console.log("User " + user.email + " received the confirmation e-mail successfully!");
                        }
                    });
                }
            });
            break;
        default:
            console.log("No action was found for " + action);
            break;
    };
});

server.listen(80, function() {
    console.log("Listening!");
});

这是我的passport.js:

var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');
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('local-login', new LocalStrategy({
        usernameField: 'email',
        passwordField: 'password',
        passReqToCallback: true
    }, function(req, email, password, done){
        User.findOne({'email': email}, function(err, user) {
            if(err) return done(err);
            if(!user) return done(null, false, null);
            if(!user.comparePasswordHashes(data.pass, user.register.password)) return done(null, false, null);
            return done(null);
        })
    }));
}

这是我的index.js路线,我的护照未定义:

module.exports = function(app, passport) {
    var indexController = app.controllers.index;
    app.get('/', indexController.index);
    app.post('/login', indexController.login);
    app.post('/register', indexController.register);

    app.post('/passport', passport.authenticate('local-login', {
        successRedirect: '/logado',
        failureRedirect: '/falhou',
        failureFlash: false
    }));

};

0 个答案:

没有答案