passport的LocalStrategy一直没起作用,求解答
发布于 9 年前 作者 fghpdf 5067 次浏览 最后一次编辑是 8 年前 来自 问答

我在以下代码第二行添加了输出信息,但是发现程序没有进来,求解答 segmentfault

##贴上相关代码 app.js

var session = require('express-session');
var bcrypt = require('bcrypt-nodejs');
var path = require('path');
var favicon = require('serve-favicon');

var logger = require('morgan');
//登陆模块涉及包
var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;

var routes = require('./routes/index');
var users = require('./routes/users');

var model = require('./database/model');



var app = express();

//登陆模块
passport.use(new LocalStrategy(function(userEmail, userPassword, done){
  console.log(userEmail, userPassword);
  new model.User({userEmail: userEmail}).fetch().then(function(data){
    var user = data;
    console.log(data);
    if(user === null){
      return done(null, false, {message: '此账号不存在'});
    } else {
      user = data.toJSON();
      if(!bcrypt.compareSync(userEmail, user.userPassword)) {
        return done(null, false, {message: '账号密码错误'});
      } else {
        return done(null, user);
      }
    }
  });
}));

passport.serializeUser(function(user, done){
  done(null, user.userEmail);
});

passport.deserializeUser(function(username, done) {
  new model.User({userEmail: user.userEmail}).fetch().then(function(user) {
    done(null, user);
  });
});

app.use(logger('dev'));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
app.use(session({secret: 'keyboard'}));
app.use(express.static(path.join(__dirname, 'public')));

路由部分index.js

var express = require('express');
var router = express.Router();
var passport = require('passport');
var bcrypt = require('bcrypt-nodejs');

var model = require('../database/model');

router.post('/login', function(req, res, next){
  console.log(req.body);
  passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/login'
  }, function(err, user, info){
    console.log(err, user, info);
    if(err) {
      return res.render('login', {title: '登录', errorMessage: err.message});
    }
    if(!user) {
      return res.render('login', {title: '登陆', errorMessage: info.message});
    }
    return req.logIn(user, function(err){
      if(err) {
        return res.render('login', {title: '登陆', errorMessage: err.message});
      } else {
        return res.redirect('/project');
      }
    });
  })(req, res, next);
});

package.json

{
  "name": "app9d",
  "version": "0.0.0",
  "private": true,
  "scripts": {
    "start": "node ./bin/www"
  },
  "dependencies": {
    "express": "4.x",
    "body-parser": "*",
    "bookshelf": "0.5",
    "cookie-parser": "*",
    "debug": "~2.2.0",
    "jade": "~1.11.0",
    "morgan": "~1.6.1",
    "serve-favicon": "~2.3.0",
    "passport": "*",
    "passport-local": "*",
    "bcrypt-nodejs": "*",
    "express-session": "*",
    "mysql": "*"
  }
} 
2 回复

自顶!!!!

我用的是Koa2框架,也出现这个问题。。。 原来使用的是 const passport = require(‘passport’) 后来使用插件koa-passport解决了这个问题: const passport = require(‘koa-passport’) 可以参考github:node-koa2-mongodb-demo passport.js

回到顶部