简体   繁体   English

护照未成功重定向

[英]Passport not successfully redirecting

I took a tutorial from the odin project on passport and it worked so I decided to implement what I learned on my own project, the difference between theirs and mine is that I was following a design pattern while they weren't and I can't seem to find the problem as to why it is not responding.我从护照上的 odin 项目中学习了一个教程,它奏效了,所以我决定实施我在自己的项目中学到的东西,他们和我的不同之处在于我遵循了设计模式,而他们没有,我不能似乎找到了为什么它没有响应的问题。 in my login page I use the email and password and I've tried changing the below code many times.在我的登录页面中,我使用了 email 和密码,并且我尝试多次更改以下代码。

this is the controller.js这是 controller.js

    //passport functions
passport.use(
  new LocalStrategy((username, password, done) => {
    User.findOne({ username: username }, (err, user) => {
      console.log(user)
      
      if (err) { 
        return done(err);
      };
      if (!user) {
        return done(null, false, { msg: "Incorrect username" });
      }
      if (user.password !== password) {
        return done(null, false, { msg: "Incorrect password" });
      }
      return done(null, user);
    });
  })
);
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});
//end ofpassport functions

    app.use(session({ secret: "cats", 
    resave: false, saveUninitialized: true }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.urlencoded({ extended: false }));
    app.use(function(req, res, next) {
      res.locals.currentUser = req.user;
      next();
    });

I then called the controller from the routes.js to authenticate the user yet it keeps failing and defaulting on the failredirecting然后我从 routes.js 调用 controller 来验证用户身份,但它一直失败并默认失败重定向

app.post('/signin', passport.authenticate("local",{
    successRedirect:"/",
    failureRedirect:'/signup'}))

By default, LocalStrategy expects to find credentials in parameters named username and password.默认情况下,LocalStrategy 期望在名为 username 和 password 的参数中找到凭据。 If your site prefers to name these fields differently, options are available to change the defaults.如果您的站点希望以不同的方式命名这些字段,则可以使用选项来更改默认值。

name of parameters in req.body should match with custom fields and LocalStrategy not called when there are empty fields usernameField and passwordField req.body中的参数名称应与自定义字段匹配,当有空字段usernameFieldpasswordField时不调用 LocalStrategy

const customFields = {
    usernameField : "email",
    passwordField : "password"
}

passport.use(
  new LocalStrategy(customFields ,(username, password, done) => {
    User.findOne({ username: username }, (err, user) => {
      console.log(user)
      
      if (err) { 
        return done(err);
      };
      if (!user) {
        return done(null, false, { msg: "Incorrect username" });
      }
      if (user.password !== password) {
        return done(null, false, { msg: "Incorrect password" });
      }
      return done(null, user);
    });
  })
);
passport.serializeUser(function(user, done) {
  done(null, user.id);
});

passport.deserializeUser(function(id, done) {
  User.findById(id, function(err, user) {
    done(err, user);
  });
});
//end ofpassport functions

    app.use(session({ secret: "cats", 
    resave: false, saveUninitialized: true }));
    app.use(passport.initialize());
    app.use(passport.session());
    app.use(express.urlencoded({ extended: false }));
    app.use(function(req, res, next) {
      res.locals.currentUser = req.user;
      next();
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM