我正在使用 passportjs 本地进行身份验证。它只是重定向到failureRedirect它没有显示任何错误,它总是重定向到failureRedirect与原始用户名和密码以及重复我的代码中的错误
我的 passport.js 档案:
const passport = require('passport');
const LocalStrategy = require('passport-local').Strategy;
const bcrypt = require('bcryptjs');
const Admin = require('../models/admin/user');
const config = require('../config/database');
module.exports = function(passport){
// With local startegy
passport.use(new LocalStrategy(function (userName, password, done){
// Match Username
let query = {userName:userName};
Admin.findOne(query, function(err, user){
console.log(user);
if(err) throw err;
if(!user){
console.log('No user Found');
return done(null, false, {message: 'No User Found'});
}
// Match Password
bcrypt.compare(password,user.password, function(err, isMatch){
if(err) throw err;
if(isMatch){
console.log('welcome to dashboard');
return done(null, user);
} else {
console.log('Wrong Password is inserted');
return done(null, false, {message: 'Wrong password'});
}
});
});
}));
passport.serializeUser(function(user, done){
done(null, user.id)
});
passport.deserializeUser(function(id, done){
Admin.findById(id, function(err, user){
done(err, user);
});
});
}
index.js 档案:
const express = require("express");
const path = require('path');
const bodyParser = require('body-parser');
const cors = require('cors');
const mongoose = require('mongoose');
const exphbs = require('express-handlebars');
const expressValidator = require('express-validator');
const multer = require('multer');
const flash = require('connect-flash');
const session = require('express-session');
const passport = require('passport');
const config = require('./config/database');
const app = express();
app.use(express.static(path.join(__dirname, 'public')));
// Routes
const admin = require('./routes/admins/a_users'); // ADMIN-Parts
const images = require('./routes/zzzimages'); // All - Images Uploading Code
// Port Number to Start
const port = 1200;
app.use(cors());
// Body parser Middleware
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: false }));
// view engine setup
app.set('views',path.join(__dirname,'views'));
app.engine(".hbs", exphbs({ defaultLayout: "mainlayout", extname: ".hbs", helpers: handlebarsHelpers }));
app.set("view engine", ".hbs");
// Express Session Middleware
app.use(session({
secret: 'keyboard cat',
resave: true,
saveUninitialized: true,
cookie: { secure: true }
}));
// Express Messages Middleware
app.use(flash());
app.use(require('connect-flash')());
app.use(function (req, res, next) {
res.locals.messages = require('express-messages')(req, res);
next();
});
// Express Validator Middleware
app.use(expressValidator({
errorFormatter: function(param, msg, value){
var namespace = param.split('.')
, root = namespace.shift()
, formParam = root;
while(namespace.length){
formParam += '[' + namespace.shift() + ']';
}
return {
param : formParam, msg : msg, value : value };
}
}));
require('./config/passport')(passport);
app.use(passport.initialize());
app.use(passport.session());
app.use('/admin',admin);
app.use('/allimg',images);
app.get('/admin-login', (req, res) => {
res.render('admin/login');
});
users.js 档案:
const express = require('express');
const router = express.Router();
const bcrypt = require('bcryptjs');
const passport = require('passport');
const config = require('../../config/database');
const Admin = require('../../models/admin/a_user');
// login process
router.post('/login', function(req, res, next) {
passport.authenticate('local', {
successRedirect:'/psc',
failureRedirect:'/admin-login',
failureFlash: true
})(req, res, next);
console.log('login is called');
});
module.exports = router;
我尝试使用console.log,如上面给出的路由/登录被调用,显示登录被称为消息而不是其他
答案 0 :(得分:0)
process.exit(0);
会阻止您的流程运行(例如php中的die
或cmd中的kill
)。您可以尝试以下方法:
passport.use("local-login", new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField: "email",
passwordField: "password",
passReqToCallback: true // allows us to pass back the entire request to the callback
}, function (req, email, password, done) {
var User = user;
var Admin = models.admin;
console.group("============================================================");
console.log("user: "+user);
console.log("user: "+password);
console.groupEnd();
var isValidPassword = function (userpass, password) {
console.log("*********************** from isvalidpassword*****************");
console.log(bCrypt.compareSync(password, userpass));
return bCrypt.compareSync(password, userpass);
};
console.log("___________________before FindOne user.findOne_________________");
console.log(email);
Admin.findOne({where: { uemail: email }
}).then(function (user) {
if (!user) {
console.log("Email Does not EXit");
return done(null, false, {message: "Email does not exist"});
}
if (!isValidPassword(user.password, password)) {
console.log("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$---from isValid Password---$$$$$$$$$$$$$$$$$$$$$$$$$$");
console.log("-------------user password: ", user.password );
console.log("-------------password: ", password );
process.exit(0);
return done(null, false, {message: "Incorrect password."});
}
var userinfo = user.get();
console.log("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&");
console.log(user);
return done(null, userinfo);
}).catch(
function (err) {
console.log("Error:", err);
return done(null, false, {
message: "Something went wrong with your Signin"
}
);
}
);
}
));