使用passport-local进行登录时遇到问题
发布于 9 年前 作者 harwinvoid 7135 次浏览 最后一次编辑是 8 年前 来自 问答

在使用passport-local进行登录时,点击登录按钮出现302错误。请各位看下问题出在哪 POST /signin 302 33.062 ms - 29 代码流程如下 $.post('/signin', param, function () {)} 这里post过去的值是{stno:' ',pwd:' '}

 app.route('/signin')
       .get(users.renderSignIn)
       .post(passport.authenticate('local',{
           successRedirect: '/',
           failureRedirect: '/signin',
           failureFlash: true
       }));

local.js

var passport = require('passport'),
    LocalStrategy = require('passport-local').Strategy,
    User = require('mongoose').model('User');

module.exports = function () {
    passport.use('local',new LocalStrategy(function (username, password, done) {
        console.log('local');
        User.findOne({
            sno: username
        }, function (err, user) {
            console.log(err);
            if (err) {
                return done(err);
            }
            if (!user) {
                return done(null, false, {
                    message: 'Unknow user'
                });
            }
            if (!user.authenticate(password)) {
                return done(null, false, {
                    message: 'Invalid password'
                });
            }
            return done(null,user);
        });
    }));
};

passport.js

var passport = require('passport'),
    mongoose = require('mongoose') ;
module.exports = function(){
    var User = mongoose.model('User');
    passport.serializeUser(function(user,done){
        console.log(user);
       done(null,user.id); 
    });
    passport.deserializeUser(function(id,done){
       User.findOne({
           _id: id,
           },'-password -salt',function(err,user){
               done(err,user);
           }); 
    });
    require('./strategies/local.js')();
};

express.js

var config = require('./config'),
    express = require('express'),
    morgan = require('morgan'),
    compress = require('compression'),
    bodyParser = require('body-parser'),
    methodOverride = require('method-override'),
    session = require('express-session'),
    flash = require('connect-flash'),
    passport = require('passport');
    
module.exports = function(){
  var app = express();
  if(process.env.NODE_ENV === 'development'){
      app.use(morgan('dev'));
  }else if(process.env.NODE_ENV === 'production'){
      app.use(compress());
  }
  
  app.use(bodyParser.urlencoded({
      extended:true
  }));
  
  app.use(bodyParser.json());
  app.use(methodOverride());
  
  app.use(session({
      saveUninitialized: true,
      resave: true,
      secret: config.sessionSecret
  }));
  
  app.set('views','./app/views');
  app.set('view engine','jade');
 
 app.use(flash());
 app.use(passport.initialize());
 app.use(passport.session());
  
  require('../app/routes/index.server.routes.js')(app);
  require('../app/routes/users.server.routes.js')(app);
  
  app.use(express.static('./public'));
  return app;  
};

app.js

process.env.NODE_ENV = process.env.NODE_ENV || 'development';
var mongoose = require('./config/mongoose'),
    express =require('./config/express'),
    passport = require('./config/passport');

var db = mongoose();
var app = express();
var  Passport = passport();
module.exports = app;
console.log('Server is running on localhost:3000/');

1 回复

问题解决了,由于我前台传过来的值是{stno:’’,pwd:’’}; 所以代码应该改成这样

passport.use('local',new LocalStrategy({
      usernameField :"stno",
      passwordField :"pwd",
	  },function(){}
	  ))
回到顶部