当我打开登录页面并提供凭据,然后successRedirect会在另一个页面上反映出来,即使我提供了错误的凭据,它也会重定向另一个页面。但是主要的问题是为什么failureFlash不显示任何消息。我正在使用ejs模板和护照js本地策略。
app.js
const express = require('express')
const mongoose = require('mongoose')
const session = require('express-session')
const expressValidator = require('express-validator')
const MongoStore = require('connect-mongo')(session)
const path = require('path')
const passport = require('passport')
const bodyParser = require('body-parser')
const flash = require('connect-flash')
const routes = require('./routes/index')
const errorHandlers = require('./handlers/errorHandlers')
require('./handlers/passport')
const app = express();
app.use(express.static(path.join(__dirname + '/public')))
app.set('views', path.join(__dirname, 'public'))
//app.engine('html', require('ejs').renderFile)
app.set('view engine', 'ejs')
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
app.use(expressValidator());
// populates req.cookies with any cookies that came along with the request
//app.use(cookieParser());
app.use(session({
secret: process.env.SECRET,
key: process.env.KEY,
resave: false,
saveUninitialized: false,
store: new MongoStore({ mongooseConnection: mongoose.connection })
}));
app.use(passport.initialize());
app.use(passport.session());
app.use(flash());
app.use((req, res, next) => {
res.locals.msg = req.flash();
res.locals.user = req.user || null;
res.locals.currentPath = req.path;
next();
});
// After allllll that above middleware, we finally handle our own routes!
app.use('/', routes);
// If that above routes didnt work, we 404 them and forward to error handler
app.use(errorHandlers.notFound);
// One of our error handlers will see if these errors are just validation errors
app.use(errorHandlers.flashValidationErrors);
// Otherwise this was a really bad error we didn't expect! Shoot eh
//if (app.get('env') === 'development') {
/* Development Error Handler - Prints stack trace */
//app.use(errorHandlers.developmentErrors);
//}
// production error handler
//app.use(errorHandlers.productionErrors);
// done! we export it so we can start the site in start.js
module.exports = app;
passport.js
const passport = require('passport');
const mongoose = require('mongoose');
const User = mongoose.model('User');
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
authController.js
const passport = require('passport');
const crypto = require('crypto');
const mongoose = require('mongoose');
const User = mongoose.model('User');
const promisify = require('es6-promisify');
exports.login = passport.authenticate('local', {
failureRedirect: '/register',
failureFlash: 'Invalid email or password',
successRedirect: '/dash',
});
exports.forgot = async (req, res) => {
// 1. See if a user with that email exists
const user = await User.findOne({ email: req.body.email });
if (!user) {
return res.redirect('/login');
}
console.log(user)
}
exports.logout = (req, res) => {
req.logout();
// req.flash('success', 'You are now logged out! ?');
res.redirect('/login');
};
exports.isLoggedIn = (req, res, next) => {
// first check if the user is authenticated
if (req.isAuthenticated()) {
next(); // carry on! They are logged in!
return;
}
req.flash('error', 'Oops you must be logged in to do that!');
res.redirect('dashhome');
};
register.ejs(and this my failureRedirect ejs file)
<!DOCTYPE html>
<html lang="en">
<head>
<title>SignUp V4</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!--===============================================================================================-->
<link rel="icon" type="image/png" href="/imagesL/icons/favicon.ico"/>
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/vendorL/bootstrap/css/bootstrap.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/fontsL/font-awesome-4.7.0/css/font-awesome.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/fontsL/iconic/css/material-design-iconic-font.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/vendorL/animate/animate.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/vendorL/css-hamburgers/hamburgers.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/vendorL/animsition/css/animsition.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/vendorL/select2/select2.min.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/vendorL/daterangepicker/daterangepicker.css">
<!--===============================================================================================-->
<link rel="stylesheet" type="text/css" href="/cssL/util.css">
<link rel="stylesheet" type="text/css" href="/cssL/main.css">
<!--===============================================================================================-->
</head>
<body>
<div class="limiter">
<div class="container-login100" style="background-image: url('imagesL/bg-01.jpg');">
<div class="wrap-login100 p-l-55 p-r-55 p-t-65 p-b-54">
<form class="login100-form validate-form" action="/register" method="POST">
<span class="login100-form-title p-b-49">
Sign Up
</span>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Who are You Representing?</span>
<input class="input100" type="text" name="user1" placeholder="Type Who You Are Representing?">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Full Name</span>
<input class="input100" type="text" name="name" placeholder="Type Your Full Name">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Company/Institution Name</span>
<input class="input100" type="text" name="user3" placeholder="Type Your Company/Institution Name">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Company/Institution No.</span>
<input class="input100" type="text" name="user4" placeholder="Type Your Company/Institution No.">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Current Address</span>
<input class="input100" type="text" name="user5" placeholder="Type Your Current Address">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Company/Institution Current Address</span>
<input class="input100" type="text" name="user6" placeholder="Type Your Company/Institution Current Address">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Active Phone No.</span>
<input class="input100" type="text" name="user7" placeholder="Type Your Active Phone No.">
<span class="focus-input100" data-symbol=""></span>
</div>
<!--
<div class="wrap-input100 validate-input m-b-23" data-validate = "Username is reauired">
<span class="label-input100">Company/Institution Phone No.</span>
<input class="input100" type="text" name="user8" placeholder="Type Your Company/Institution Phone No.">
<span class="focus-input100" data-symbol=""></span>
</div>
-->
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Active Email ID</span>
<input class="input100" type="email" name="email" placeholder="Type Your Active Email ID">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Company/Institution Active Email ID</span>
<input class="input100" type="email" name="user10" placeholder="Type Your Company/Institution Active Email ID">
<span class="focus-input100" data-symbol=""></span>
</div>
<span class="login100-form-title p-b-49">
For KYC
</span>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">Aadhar Number</span>
<input class="input100" type="number" name="user11" placeholder="Type Your Aadhar Number">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input m-b-23" data-validate = "required field is empty">
<span class="label-input100">PAN Number</span>
<input class="input100" type="string" name="user12" placeholder="Type Your PAN Number">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="validate-input m-b-23" data-validate = "required field is empty">
<input type="checkbox" name="userTerm1">
<span name="userTerm11">I agree to the Terms of Use</span>
</div>
<div class="validate-input m-b-23" data-validate = "required field is empty">
<input type="checkbox" name="userTerm2" >
<span name="userTerm22">I agree to the Terms and Conditions</span>
</div>
<div class="wrap-input100 validate-input" data-validate="Password is required">
<span class="label-input100">Password</span>
<input class="input100" type="password" name="password" placeholder="Type Your Password">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="wrap-input100 validate-input" data-validate="Password is required">
<span class="label-input100">Confirm Password</span>
<input class="input100" type="password" name="confirmPassword" placeholder="Type Your Password">
<span class="focus-input100" data-symbol=""></span>
</div>
<div class="text-right p-t-8 p-b-31">
<a href="#">
</a>
</div>
<div class="container-login100-form-btn">
<div class="wrap-login100-form-btn">
<div class="login100-form-bgbtn"></div>
<button class="login100-form-btn" type="submit">
Sign Up
</button>
</div>
</div>
<div class="txt1 text-center p-t-54 p-b-20">
<span>
Or Sign Up Using
</span>
</div>
<div class="flex-c-m">
<a href="#" class="login100-social-item bg1">
<i class="fa fa-facebook"></i>
</a>
<a href="#" class="login100-social-item bg2">
<i class="fa fa-twitter"></i>
</a>
<a href="#" class="login100-social-item bg3">
<i class="fa fa-google"></i>
</a>
</div>
</div>
</form>
</div>
</div>
</div>
<div id="dropDownSelect1"></div>
<!--===============================================================================================-->
<script src="/vendorL/jquery/jquery-3.2.1.min.js"></script>
<!--===============================================================================================-->
<script src="=/vendorL/animsition/js/animsition.min.js"></script>
<!--===============================================================================================-->
<script src="/vendorL/bootstrap/js/popper.js"></script>
<script src="/vendorL/bootstrap/js/bootstrap.min.js"></script>
<!--===============================================================================================-->
<script src="/select2/select2.min.js"></script>
<!--===============================================================================================-->
<script src="/vendorL/daterangepicker/moment.min.js"></script>
<script src="/vendorL/daterangepicker/daterangepicker.js"></script>
<!--===============================================================================================-->
<script src="/vendorL/countdowntime/countdowntime.js"></script>
<!--===============================================================================================-->
<script src="/jsL/main.js"></script>
</body>
</html>
答案 0 :(得分:0)
在您的app.js
文件中,将res.locals.msg = req.flash();
替换为
res.locals.errorMsg = req.flash("error");
然后在您想显示它的任何地方的ejs文件中添加这段代码。
<% if (errorMsg) { %>
<p><%= errorMsg %></p>
<% } %>
如果要显示成功即时消息,请在res.locals.successMsg = req.flash("success");
之后添加
error
,并以相同的方式在ejs文件中显示它们< / p>
<% if (successMsg) { %>
<p><%= successMsg %></p>
<% } %>