2015-04-13 1 views
5

kann jemand bitte helfen Sie mir mit diesem. Ich bekomme immer Error: passport.initialize() middleware not in use, wenn ich versuche, eine Verbindung zu einem Benutzer herzustellen, der sich in der Datenbank befindet. Ausnahmefälle funktionieren gut (e.i falsches Passwort/Benutzername nicht gefunden). Hierpass.initialize() Middleware nicht für Express 4.10 für benutzerdefinierten Rückruf

ist das Setup:

Versionen i

// version 
node --version v0.10.33 
[email protected] 
[email protected] 
[email protected] 

server.js Code verwende hier

// server.js 
'use strict'; 
var express = require('express'); 
var bodyParser = require('body-parser'); 
var session = require('express-session'); 
var passport = require('passport'); 
var flash = require('connect-flash'); 
var cookieParser = require('cookie-parser'); 

//Our custom modules 
var mysqlc = require('./modules/mysql_client'); 

// Our custom apps 
var app = express(); 

//view engine setup 
app.set('env', process.env.NODE_ENV); 
console.log('Running as environment: ' + app.get('env')); 
app.set('views', path.join(__dirname, 'views')); 
app.set('view engine', 'jade'); 

app.use(favicon(path.join(__dirname,'./public/favicon.ico'))); 
app.use(bodyParser.json()); 
app.use(bodyParser.urlencoded({ extended: false })); 
app.use('/rootpath', express.static(path.join(__dirname,'./public'))); 

// use session/cookie for auth 
app.use(cookieParser()); 
app.use(session({ 
    secret: 'clinksecret', // TODO: STORE outside 
    saveUninitialized: true, 
    resave: true 
})); 

// use connect-flash for flash messages stored in session 
app.use(flash()); 

var server = require('http').Server(app); 
var io = require('socket.io')(server); 

/** 
* Connect to all servers 1st 
* */ 

// mysqlc connection setup 
var connPropMySqlC = { 
     connectionLimit : 10, 
     host   : config.settings.auth.clink.host, 
     user   : config.settings.auth.clink['user'], 
     password  : config.settings.auth.clink['password'], 
     database  : config.settings.auth.clink.database 
}; 
var mydbc = new mysqlc.MySqlClient(connPropMySqlC); 
mydbc.connect(function (err) { 
    if (err) { 
     console.error('server-mdbc-connect-ERROR:', err); 
     throw err; 
    } 
}); 

//authentication with session after connection to mydbc 
var passport = require('./modules/auth')({mydbc:mydbc, io:io, app:app, passport:passport}); 
app.use(passport.initialize()); 
app.use(passport.session()); 

/* 
* routers 
* */ 
var testapp = require('./testapp/app')({mydbc:mydbc, io:io}); 
app.use('/testapp', testapp); 

auth.js (alle Pass configs)

// auth.js 
/** 
* for user authentication using passport 
*/ 
var LocalStrategy = require('passport-local').Strategy; 
var bcrypt = require('bcrypt-nodejs'); 

//Variables local to module 
var io; 
var mydbc; 
var app; 
var passport; 

function validPassword(val, hash, callback) { 
    return bcrypt.compare(val, hash, callback); 
} 

function exportModFunc(args) { 
    /** 
    * used to passing object across modules 
    * @param {object} args: args = {mydbc, io} // where mydbc mysqlc.MySqlClient object 
    * @return {object} passport 
    */ 
    mydbc = args.mydbc; // update variable 
    io = args.io;  // update variable 
    app = args.app; // update variable 
    passport = args.passport; // update variable 

    // route middleware 
    passport.use('local-login', new LocalStrategy({ 
     usernameField : 'formLogin_user', 
     passwordField : 'formLogin_password', 
     passReqToCallback : true // allows us to pass back the entire request to the callback 
    }, 
    function(req, username, password, done) { 
     mydbc.getUser({'username':username}, function(err, user) { 
      console.log('test-local-login', username, password, done, err, user) 
      if (err) 
       return done(err); 

      // if no user is found, return the message 
      if (!user) 
       return done(null, false, req.flash('loginMessage', 'No user found.')); // req.flash is the way to set flashdata using connect-flash 

      // if the user is found but the password is wrong 
      validPassword(password, user.password, function(err, res) { 
       if (err) 
        return done(err); 

       if (!res) { 
        return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.')); // create the loginMessage and save it to session as flashdata 
       } else { 
        // all is well, return successful user 
        return done(null, user); 
       } 
      }); 
     }); 
    })); 

    passport.serializeUser(function(user, done) { 
     /** 
     * each session will serialize to userid 
     * http://passportjs.org/guide/configure/ 
     * @param {object} user: same as object as returned by mydbc.getUser() 
     * @return {null} null 
     */ 
     done(null, user.userid); 
    }); 

    passport.deserializeUser(function(userid, done) { 
     /** 
     * each session will deserialize to user 
     * http://passportjs.org/guide/configure/ 
     * @param {int} userid: same as mydbc.getUser().userid 
     * @return {null} null 
     */ 
     mydbc.getUser({'userid':userid}, function(err, user) { 
      done(null, user); 
     }); 
    }); 

    // route post request 
    //// process the login form 
    app.post('/authLogin/', function(req, res, next) { 
     passport.authenticate('local-login', function(err, user, info) { 
      if (err) { return next(err); } 
      var errors = {}; 
      var loginMsg = req.flash('loginMessage'); 
      if (loginMsg.length !== 0 || (!user)) { 
       errors.loginMsg = loginMsg; 
       return res.json({ 
        errors: errors 
       }); 
      } 
      console.log('test-authLogin-local-login', err, user, info); 
      req.logIn(user, {failureFlash: true}, function(err) { 
       if (err) { return next(err); } 
       return res.redirect('/users/' + user.username); 
      }); 
     })(req, res, next); 
    }); 

    return passport; 
}; 

module.exports = exportModFunc; 

Antwort

19

Sie sollten initiali ze Pass, bevor es zu Ihrer Route Middleware hinzufügen: http://passportjs.org/guide/configure/

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

//authentication with session after connection to mydbc 
var passport = require('./modules/auth')({mydbc:mydbc, io:io, app:app, passport:passport}); 

Sie neu definieren auch Ihren Pass var in server.js, die nicht notwendig ist.

+0

vielen dank :) – abarik

Verwandte Themen