护照认证总是失败

时间:2018-03-13 13:25:14

标签: node.js mongodb express passport.js

我尝试使用快速框架,mongodb和护照设置一个简单的登录页面进行身份验证。 我的身份验证总是进入failstate。我哪里错了?

my sever.js:

var express = require("express");
var app = express();
var bodyParser = require('body-parser');
var cors = require('cors');
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
//var router = express.Router();
var path = require('path');
var mongoose = require('mongoose');
var User = require('./model');
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());





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


passport.use(new LocalStrategy(
  function(username, password, done) {

      users.findOne({
         username : 'username'
      }, function(err, user) {

        if (err) {
          return done(err);
        }

        if (!user) {
          return done(null, false); 
        }

        if (user.password != password) {
          return done(null, false);
        }
        return done(null, user);
      });
  }
));

app.post('/login',
  passport.authenticate('local', { failureRedirect: '/error' }),
  function(req, res) {
    res.redirect('/success?username='+req.user.username);
});




//app.use("/",router);

//app.use(bodyParser());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());
app.use(cors());




app.get('/success', (req, res) => res.send("Welcome "+req.query.username+"!!"));
app.get('/error', (req, res) => res.send("error logging in"));//ALWAYS LAND HERE 

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

/*passport.deserializeUser(function(id, cb) {
  User.findById(id, function(err, user) {
    cb(err, user);
  });
});
*/










//app engine
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));


mongoose.connect('mongodb://localhost:27017/UserSchema');

/*
router.use(function (req,res,next) {
  console.log("/" + req.method);
  next();
});*/

app.get("/",function(req,res){
  res.render('index');
});

app.post("/user",function (req, res) {
    var u = new User();
     //console.log(req.body);
    u.email = req.body.email;
    u.name = req.body.name;
    u.password = req.body.password;
    //p.photo = req.body.photo;
    u.save(function (err) {
        if (err) {
            res.send(err);
        }
        res.render('login');
});

});


app.get("/login",function (req, res) {
    res.render('login');

});




app.get("/all",function (req, res) {
    User.find(function (err, products) {
        if (err) {
            res.send(err);
        }
        res.send(products);
    });
});

/*
router.get("/about",function(req,res){
  res.sendFile(path + "about.html");
});

router.get("/contact",function(req,res){
  res.sendFile(path + "contact.html");
});


app.use("*",function(req,res){
  res.sendFile(path + "404.html");
});
*/












app.listen(3000,function(){
  console.log("Live at Port 3000");
});

对不起垃圾评论

这是我的mongoose架构(model.js):

var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var UserSchema = new Schema({
    email: String,
    name: String,
    password: String,
    //photo: String,
});
module.exports = mongoose.model('User', UserSchema);

我的login.ejs:

<!DOCTYPE html>
<html>
<<title>Login</title>
<body>


<form action="/login", method="post">
  Email:<br>
  <input type="text" name="username">
  <br>
  Password:
  <br>
  <input type="text" name="password">
  <br><br>
  <input type="submit" value="Submit">
</form> 


</body>
</html>

和我的注册页面,可以在db:

中注册和插入
<!DOCTYPE html>
<html>
<body>

<form action="/user", method="post">
  Email:<br>
  <input type="text" name="email">
  <br>
  Name:<br>
  <input type="text" name="name" >
  <br>
  Password:
  <br>
  <input type="text" name="password">
  <br><br>
  <input type="submit" value="Submit">
</form> 

<a href="/login"/><input  type="submit" value="Login">
</body>
</html>

1 个答案:

答案 0 :(得分:0)

您将MongoDB 用户模型导入为:

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

但是,您在&#34;用户&#34;

上调用 .findOne()方法
passport.use(new LocalStrategy(
  function(username, password, done) {

      users.findOne({
         username : 'username'
      }, function(err, user) {

不应该是User.findOne()

如果这还没有解决问题,那么你应该详细说明你的意思&#34; 我的应用程序总是转到failstate &#34;。