찾다

 >  Q&A  >  본문

node.js - passport的LocalStrategy一直没起作用

我在以下代码第二行添加了输出信息,但是发现程序没有进来,求解答
环境:windows 10
包的版本

数据库读取正常,router.post可以获取到req.body的值,没有问题,同时也获得到err user info
值分别为

err: null
user: false
info: { message: 'Missing credentials' }

贴上相关代码
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": "*"
  }
}
阿神阿神2770일 전411

모든 응답(1)나는 대답할 것이다

  • 怪我咯

    怪我咯2017-04-17 13:34:53

    已经知道原因了,因为我使用的email注册,所以要设置usernameField

    회신하다
    0
  • 취소회신하다