【问题标题】:convert passport usage from mongoose to sequelize将护照使用从猫鼬转换为续集
【发布时间】:2018-09-13 00:49:06
【问题描述】:

我正在构建一个需要在登录期间进行身份验证的应用程序,为此我使用了护照(passport-local)。该应用程序不会为登录部分运行,并且在删除所有语法错误后可用的最后一个选项是我使用护照的方式(如他们的文档中给出的)是在我使用 sequelize 时用于猫鼬的。有人可以告诉我如何纠正我的 passport.js 文件,以便它也适用于续集吗? (通过 sequelize 使用 mysql;数据库已经填充) 这是我的 passport.js 文件

const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const Users = require('./db').Users

module.exports = function (passport) {
  console.log("passport is working");

  passport.serializeUser(function (users, done) {
    console.log("Serialize");
    done(null, users.id)
  })

  function findById(id, fn) {
    User.findOne(id).exec(function (err, user) {

      if (err) {
        return fn(null, null);
      } else {
        return fn(null, user);
      }
    });
  }

  passport.deserializeUser(function (id, done) {
    console.log("DeSerialize");
    findById(id, function (err, user) {
      console.log(user);
      done(err, user);
    });
  })

  passport.use(new LocalStrategy(
    function (username, password, done) {
      Users.findOne({ where: { username: username } },
        function (err, users) {
          if (err) { return done(err); }
          if (!users) {
            return done(null, false, { message: 'Incorrect username.' });
          }
          if (!users.validPassword(password)) {
            return done(null, false, { message: 'Incorrect password.' });
          }
          return done(null, users);
        });
    }
  ));
}

这是我的 db.js 文件

const Sequelize = require('sequelize');

const db = new Sequelize(
  'mydb',
  'myuser',
  'mypass',
  {
    dialect: 'mysql',
    host: 'localhost',
    pool: {
      max: 8,
      min: 0,
      aquire: 30000,
      idle: 10000
    },
  }
);

const Users = db.define('users', {
  email: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true
  },
  username: {
    type: Sequelize.STRING,
    allowNull: false,
    unique: true,
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false,
  }

})


db.sync().then(() => console.log("Database is ready"))

exports = module.exports = {
  db,
  Users
}

另外,作为旁注,我可以在使用 passport.authenticate 发布请求后进行 res.send 吗?

app.post('/login', passport.authenticate('local'),
function(req, res) {
    res.send({
        url:'/profile',
        username:req.body.username
    });
  });

提前致谢!!

【问题讨论】:

    标签: node.js express mongoose sequelize.js passport.js


    【解决方案1】:

    findById 在 Sequelize 4 中已被 findByPk 方法替换。

    http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-findByPk

    【讨论】:

      【解决方案2】:

      是的,您需要更改获取用户的函数。这: function findById(id, fn)

      应该是这样的:

      function findById(id, fn) {
          User.findById(id).then((user) {
            return fn(null, user);
          });
        }
      

      你还需要稍微改变一下:

      passport.use(new LocalStrategy(
          function (username, password, done) {
            Users.findOne({ where: { username: username } })
              .then((user) => {
                if (!user) {
                  return done(null, false, { message: '' });
                }
      
                // validate your password**
                return done(null, user);
              });
          }
        ));
      

      如果你想使用像user.validatePassword() 这样的函数,你需要定义一个实例方法(查看sequelize docs)或here

      .

      【讨论】:

      • 试过还是一样的结果
      • 在控制台中既不序列化也不反序列化。
      • 在终端中它只是将输入参数转换为sql查询字符串,但不提供输出结果。查询是正确的,因为当我硬编码到我的 sql 数据库中时,我得到了正确的结果。只有当我从续集开始时才不会发生。
      • 尝试删除这个变量module.exports = function (passport) { 你不需要将它传递给函数。你已经在上面定义了
      • www.github.com/leoking-crey/webdev_project/tree/debug
      猜你喜欢
      • 2015-05-05
      • 1970-01-01
      • 2017-06-30
      • 2017-04-02
      • 2018-07-31
      • 2015-03-29
      • 2021-10-24
      • 1970-01-01
      • 2017-10-24
      相关资源
      最近更新 更多