我正在尝试使用Express,Node和React-Router建立登录路由。
passport.authenticate没有被调用。 console.log
尚未注册到控制台。有人可以帮我弄清楚为什么以及如何解决它吗?预先感谢。
server.js
const express = require('express');
const app = express();
const bodyparser = require("body-parser");
const passport = require("passport");
const session = require("express-session");
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// bodyparser middleware
app.use(bodyparser.urlencoded({ extended: false }));
app.use(bodyparser.json());
// import passport-config file
require("./passport-config")(passport);
// express-session middleware
app.use(session({
secret: process.env.SECRET,
resave: false,
saveUninitialized: false
}));
// passport middleware
app.use(passport.initialize());
app.use(passport.session());
const routes = require('./routes.js');
routes(app);
// listen for requests :)
const listener = app.listen(process.env.PORT, () => {
console.log('Your app is listening on port ' + listener.address().port);
});
routes.js
module.exports = (app, database) => {
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
const db = mongoose.connection;
app.post('/login', (req, res, next) => {
console.log("login"); // registering to console
passport.authenticate("local", {
successRedirect: "/dashboard",
failureRedirect: "/login"
})(req, res, next);
console.log("login2"); // registering to console
});
}
passport-config.js文件
mongoose.connect(process.env.DATABASE, { useNewUrlParser: true });
const db = mongoose.connection;
module.exports = (passport) => {
console.log("passport"); // <- not registering in console
passport.use(new LocalStrategy((e_mail, pass, done) => {
console.log(e_mail); // <- not registering in console
console.log(pass); // <- not registering in console
let email = e_mail.toLowerCase();
let query = { email: email };
User.findOne(query, (err, user) => {
if (err) { console.log(err); }
if (!user) {
return done(null, false, { message: "No user found or password incorrect" });
}
bcrypt.compare(pass, user.password, (error, isMatch) => {
if (error) { console.log(error); }
else {
if (isMatch) {
return done(null, user);
}
else {
return done(null, false, { message: "No user found or password incorrect" });
}
}
});
});
}));
passport.serializeUser((user, done) => {
done(null, user._id)
});
passport.deserializeUser((id, done) => {
User.findById(id, (err, user) => {
done(err, user);
});
});
};
编辑 可能与我的webpack-config文件有关吗?
webpack-config.js
const path = require('path');
module.exports = {
mode: 'production',
context: path.join(__dirname, './'),
entry: './app/app.jsx',
output: {
path: path.join(__dirname, 'public'),
filename: 'bundle.js',
},
resolve: {
extensions: ['.js', '.jsx'],
},
module: {
rules: [
{
test: /\.jsx?$/,
// loader: 'jsx-loader',
use: {
loader: 'babel-loader'
},
exclude: /node_modules/,
include: path.join(__dirname, 'app'),
},
],
},
};
答案 0 :(得分:0)
如果您正在使用用户名密钥email
和密码密钥pass
,那么尝试使用该密钥怎么办?
passport.use(new LocalStrategy({
usernameField: 'e_mail',
passwordField: 'pass'
},
(e_mail, pass, done) => {
// ...
}
));