2016-07-25 79 views
0

已經與這個問題摔跤2天了:)現在感謝一些幫助。passportjs本地策略不能獲得路線

我試圖建立與passportjs +源碼+ sequelize快遞認證,

1),當我把上面app.use(passport.session())登錄路線行不通

2)當我把下面passport.session路線應用程序不能得到的路線

App.js 



    --- normal includes 
app.use(cookieParser()); 
app.use(session({ 
    secret: 'ilovescotchscotchyscotchscotch', 
    resave: true, 
    saveUninitialized: true 
})); 

var passport = require('passport'); 

app.use(passport.initialize()); 

app.use(passport.session()); 

var router = require('./routes/index')(passport); 

app.use('/', router); 

var initPassport = require('./passp.js'); 
initPassport(passport); 

護照文件

// load all the things we need 
var LocalStrategy = require('passport-local').Strategy; 

// expose this function to our app using module.exports 
module.exports = function(passport) { 
    var User = require('./dbCon.js'); 
    passport.serializeUser(function(user, done) { 
     done(null, user.dataValues.id); 
    }); 

// used to deserialize the user 
    passport.deserializeUser(function(id, done) { 
     User.findOne({where : {id : id}}).then(function(user) { 
      done(null, user); 
     }).catch(function(err){ 
      console.log(err) 
     }); 
    }); 


    passport.use('local-signup', new LocalStrategy({ 
      // by default, local strategy uses username and password, we will override with email 
      usernameField : 'email', 
      passwordField : 'password', 
      passReqToCallback : true // allows us to pass back the entire request to the callback 
     }, 
     function(req, email, password, done) { 
      User.findOne({where : {username : email } }) 
       .then(function(user){ 
        if(user === null) { 
         User.create({ 
          username : email, 
          password : password 
         }).then(function(user){ 
          done(null, user); 
         }).catch(function(err){ 
          console.log(err) 
         }) 
        } 
        else{ 
         done(null, false); 
        } 

       }).catch(function(err){ 
       console.log(err) 
      }) 

     } 
    )) 

    passport.use('local-login', new LocalStrategy({ 
      // by default, local strategy uses username and password, we will override with email 
      usernameField : 'email', 
      passwordField : 'password', 
      passReqToCallback : true // allows us to pass back the entire request to the callback 
     }, 
     function(req, email, password, done) { // callback with email and password from our form 
      return User.findOne({where : {username : email } }) 
       .then(function (user) { 
        if(user === null) done(null, true) 
        if(user.dataValues.password === password) done(null, user) 
        else{ 
         done(null, true) 
        } 

       }) 
     }) 
    ) 

}; 

路由文件

module.exports = function(passport){ 

router.get('/', function(req, res, next) { 
    res.render('index', { title: 'Express' }); 
}); 

router.get('/error', function(req, res, next) { 
    res.render('error', { title: 'Express' }); 
}); 

router.get('/admin', function(req, res, next) { 
    res.render('admin', { title: 'Express' }); 
}); 


router.post('/signup', function(req, res, next) { 
    passport.authenticate('local-signup', function(err, user, info) { 
     if (err) { return next(err); } 
     // Redirect if it fails 
     if (!user) { return res.redirect('/'); } 
     req.logIn(user, function(err) { 
      if (err) { return next(err); } 
      // Redirect if it succeeds 
      return res.render('admin'); 
     }); 
    })(req, res, next); 
}); 

router.post('/login', function(req, res, next) { 
    passport.authenticate('local-login', function(err, user, info) { 
     if (err) { return next(err); } 
     // Redirect if it fails 
     if (!user) { return res.redirect('/'); } 
     req.logIn(user, function(err) { 
      if (err) { return next(err); } 
      // Redirect if it succeeds 
      return res.render('admin'); 
     }); 
    })(req, res, next); 
}); 

return router 

} 請幫幫我!謝謝!

回答

1

.then()免費獲贈結果作爲第一個參數,而不是error

試試:

// used to deserialize the user 
passport.deserializeUser(function(id, done) { 
    User.findOne({where : {id : id}}) 
    .then(function(user) { 
     done(null, user); 
    }) 
    .catch(function(err){ 
     done(err, null); 
    }); 
}); 
+0

附:你可以做'User.findById(id)'來獲取用戶 –

+0

感謝您的回覆!但我仍然獲得500 GET/admin – Skaplun

+0

nodejs控制檯上是否有任何消息? –