使用带有Reactjs的passportjs本地策略

时间:2017-10-05 20:13:53

标签: reactjs authentication passport.js passport-local

我正在努力找出使用Passport和我的React App进行日志记录时出错的地方

下面是我的代码,我放了很多console.log来弄清楚它的去向。 当我从网络上激活日志功能时,它能够命中1,2,并且能够在3点击到路由器。

这与我输入的电子邮件或密码无关。如果我的数据库中有电子邮件,或者密码是否正确。任何帮助,将不胜感激。

这是反应组件的登录功能

logIn () {
    console.log('111111111 log in fired')
    let user = {
      email: this.refs.email.value,
      password: this.refs.password.value
    }

    console.log('22222222222 created user', user)

    axios.post(`/users/login`, user)
    .then((res) => {
      console.log('8888888888 got a response', res)
      if (res.data.message) {
        console.log('9a9a9a9a9a9a got an errorMessage')
        this.setState({
          errorMessage: res.data.message
        })
      } else if (res.data.user) {
        console.log('9b9b9b9b9b9b9 got a user back', res.data.user)
        window.location.href = '/#/Home'
      } else {
        console.log('9c9c9c9c9c9c9c9c got nothing back', res.data)
        this.setState({
          errorMessage: ''
        })
      }
    })
    .catch((error) => {
      console.log('88f8f8f8f8f8f8f8 got an error')
      console.log('axios error', error)
    })
  }

这是用户路线

const express = require('express')
const app = express();
const router = express.Router()
const bodyParser = require('body-parser')
const cookieParser = require('cookie-parser');
const db = require('./../models')
const User = db.User

const bcrypt = require('bcrypt')

router.use(bodyParser.json({ extended: false }))
router.use(cookieParser());

const passport = require('passport')
const LocalStrategy = require('passport-local').Strategy
const session = require('express-session');
const CONFIG = require('../config/config.js');

app.use(session(CONFIG.SESSION));
app.use(passport.initialize());
app.use(passport.session());

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

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

passport.use(new LocalStrategy(
  function(email, password, done) {
    console.log('44444444444')
    User.findOne({
      where: {
        email: email
      }
    })
    .then(function(user){
      console.log('55555555555 user', user)

      bcrypt.compare(password, user.password, function(err, res){
        console.log('666666666 compared password and hash')
        if(err) {
          console.log('EEERRRROOOOOOORRRRR', err)
          return done (err)
        }
        if (!user) {
          console.log('NNNOOOOOO UUUUSSSSEEEEERRRRR')
          return done (null, false, {message: 'Incorrect Username'})
        }
        if (!res) {
          console.log('IINNVVAAALLLIIIDDDD PPPAAAAASSSWWWOOORRRDDD')
          return done (null, false, {message: 'Incorrect Password'})
        }
        console.log('77777777 user')
        return done(null, {user})
      })
    })
  }
));

router.post('/login', function(req, res){console.log('3333333333')},     
passport.authenticate('local', {successRedirect: '/Home', failureRedirect:'/LogIn', failureFlash: true}), function(req, res) {
  res.redirect('/Home')
})

再次,console.log只能在网络端点击1,2, 在服务器端点击3 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

已经有一段时间了,但是对某人可能有用。 护照验证回调期望接收用户名和密码作为参数。要更改此设置,我们必须向LocalStrategy构造函数添加选项:

passport.use(new LocalStrategy({
    usernameField: 'email'
}, function(email, password, done) {
    // ...
   }
));