How to send flash messages in Express 4.0?

后端 未结 6 2419
梦谈多话
梦谈多话 2020-11-28 03:26

So my web application requires authentication, and I have a signup page where if the person tries to sign up with an email that is already in the database, I want to show th

6条回答
  •  眼角桃花
    2020-11-28 03:51

    After researching for two days and wanting to give up A LOT I've finally found out how to use connect-flash (u do not need cookie-parser) a few main things use (return res.redirect) instead of res.render it does not like render for callbacks I don't know why. Take a look at my code to get a visual.

    app.js

    var express                 = require("express"),
        bodyParser              = require("body-parser"),
        mongoose                = require("mongoose"),
        passport                = require("passport"),
        LocalStratagy           = require("passport-local"),
        User                    = require("./user"),
        passportLocalMongoose   = require("passport-local-mongoose"),
        flash                   = require('connect-flash'),
        app                     = express();
        //using express-session
    app.use(require("express-session")({
        secret:"The milk would do that",
        resave: false,
        saveUninitialized: false
    }));
    app.use(flash());
    
    app.use(function(req, res, next){
        res.locals.message = req.flash();
        next();
    });
    
    
    //connectiong to a specific database
        mongoose.connect("mongodb://localhost/LoginApp");
    
    
        //so body-parser works
    app.use(bodyParser.urlencoded({extended: true}));
    
    //making it so express uses the public dir
    app.use(express.static("public"));
    
    //setting the view engine to ejs
    app.set("view engine", "ejs");
    
    
    // so passport works
    app.use(passport.initialize());
    app.use(passport.session());
    
    //authenticated data from the login form
    passport.use(new LocalStratagy(User.authenticate()));
    
    //reading the data and encoding it
    passport.serializeUser(User.serializeUser());
    
    //reading the data and unencoding it
    passport.deserializeUser(User.deserializeUser());
    
    
    //ROUTES
    app.get("/", function(req, res){
        res.render("index");
    });
    
    
    // AUTH ROUTES
    
    //show login
    app.get("/login", function(req, res){
        req.flash("error", "")
        res.render("Login");
    });
    
    //handle login form data
    app.post("/login", passport.authenticate("local",{
        failureRedirect: "/login",
        failureFlash: true,
    }) ,function(req, res){
        req.flash("success", "Logged in");
        return res.redirect("/");
    });
    
    //Show signup form
    app.get("/signup", function(req, res){
        res.render("Signup");
    });
    
    //handle signup form data
    app.post("/signup", function(req, res){
        User.register(new User({username: req.body.username}), req.body.password, function(err, user){
            if(err){
                req.flash("error", err.message);
                return res.redirect("/signup");
            }
            passport.authenticate("local")(req, res, function(){
                req.flash("success", "successfuly Signed up");
                return res.redirect("/");
            });
        });
    });
    
    
    
    app.listen(3000, function(){
        console.log("server started");
    });
    

    Header.ejs

      
    
        
            
            
            
            
            
            
            
    
            
            
            
            wire frame chal
        
        
    
            

    <%= message.error %>

    <%= message.success %>

    Login.ejs

       <% include ../partials/header %>
    
    <% include ../partials/footer %>

    Signup.ejs

      <% include ../partials/header %>
    
    
    <% include ../partials/footer %>

提交回复
热议问题