如何将身体传递到护照本地?

时间:2017-12-12 12:21:59

标签: mongodb express mongoose passport.js passport-local

我正在关注一些视频课程,他们向我们展示了如何使用Passport进行Google OAuth,最后他们就像#34; Welp,现在您可以使用所有策略了!玩得开心!"

所以我试图像这样实施本地护照:

// passport.js
// ...
passport.use(
  new LocalStrategy((username, password, done) => {
    User
      .findOne({ username, password })
      .then(userFromDB => {
        if (userFromDB) {
          done(null, userFromDB) // No errs so return the user from db
        } else {
          new User({
            username,
            password,
          })
            .save() // Add user to our db
            .then(userFromDB => done(null, userFromDB)) // return user from db
        }
      })
  })
)
// ...

// routes.js
// ...
app.post(`/auth/local`,
  passport.authenticate(`local`, {
    failureRedirect: `/api/v1/current_user`,
  }),
  (req, res) => res.send(`It worked`)
)
// ...

我的问题是new LocalStrategy((username, password, done) ...如何获得usernamepassword

这是一台带Mongo的Express服务器。

1 个答案:

答案 0 :(得分:0)

 app.post('/login', 
      passport.authenticate('local', { failureRedirect: '/login' }),
      function(req, res) {
        res.redirect('/');
      });
  

当你点击路线/login然后passport.authenticate('local'   自动调用函数passport.use(new LocalStrategy( .in   从前端登录路由您需要发送用户名和密码

passport.use(
  new LocalStrategy((username, password, done) => {
    User
      .findOne({ username, password })
      .then(userFromDB => {
        if (userFromDB) {
          done(null, userFromDB) // No errs so return the user from db
        } else {
          new User({
            username,
            password,
          })
            .save() // Add user to our db
            .then(userFromDB => done(null, userFromDB)) // return user from db
        }
      })
  })
)