简体   繁体   English

req.flash 不是 function 快速护照

[英]req.flash is not a function express passport

I am trying to authenticate the user using passport.I am trying to display message using flash.In the passport-config.js,I wrote the local strategy.When I run the code,it give an error "req.flash is not a function " in passport-config.js file.This the first time I am using passport and flash.Please help me.Thanks in advance.我正在尝试使用护照对用户进行身份验证。我正在尝试使用 flash 显示消息。在 passport-config.js 中,我编写了本地策略。当我运行代码时,它给出一个错误“req.flash 不是function“在passport-config.js文件中。这是我第一次使用护照和flash。请帮助我。提前致谢。 app.js应用程序.js

const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const passport = require("passport");
const initializePassport=require('./passport-config')(passport);
const auth=require('./authenticate');
const bcrypt=require('bcrypt');
const session = require('express-session');
const flash = require('express-flash');
const Post=require("./Post");
const User=require('./User');
const app = express();
app.set('view engine', 'ejs');
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended: true}));
app.use(express.static("public"));
app.use(session({
  secret:"secret",
  resave:false, //this means session variables are not resaved if nothing is changed
  saveUninitialized:false //this means we dont want to save empty value in session if there is no value
}))
app.use(flash());
app.use(passport.initialize());
app.use(passport.session());
app.use(function(req, res, next) {
  // make all error and success flash messages available from all templates
  res.locals.error = req.flash("error")
  res.locals.success = req.flash("success")
  next()
})

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

app.post("/login",passport.authenticate('local',{
  successRedirect:'/',
  failureRedirect:'/login',
  failureFlash:true
}))

passport-config.js护照-config.js

const LocalStrategy = require('passport-local').Strategy;
const mongoose=require('mongoose');
const bcrypt = require('bcrypt');
const User=require('./User');
function initialize(passport) {
  const authenticateUser =(req,email, password, done) => {
    User.findOne({email:email}).then(user=>{
      if(!user){
      //   req.flash('error','This email is not registered');
        return done(null,false,req.flash('error','This email is not registered'));
      }
      //Match password
      bcrypt.compare(password,user.password,(err,isMatch)=>{
        if(err){
           //  req.flash('error','Something went wrong!Please try again.');
          throw err;
        }
        if(isMatch){
          return done(null,user); 
        }
        else{
           req.flash('error','Password Incorrect');
          return done(null,false, req.flash('error','Password Incorrect'));
        }
      });
    }).catch(err=> console.log(err));
  }

  passport.use(new LocalStrategy({ usernameField: 'email' ,passReqToCallBack: true}, authenticateUser));
  passport.serializeUser((user, done) => done(null, user.id));
  passport.deserializeUser((id, done) => {
    User.findById(id,(err,user)=>{
    done(err,user);
    })
  })
}

module.exports = initialize;

Try putting passport before the flash试着把护照放在 flash 之前

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

passport.use(new LocalStrategy({ usernameField: 'email', passReqToCallback: true}, authenticateUser)); passport.use(new LocalStrategy({ usernameField: 'email', passReqToCallback: true}, authenticateUser)); I just had to add passReqToCallback: true.It works now.我只需要添加 passReqToCallback: true。它现在可以工作了。

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

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