[英]Passport JS - Login always going through the failureRedirect route
I am currently developing a web application of my own.我目前正在开发自己的 web 应用程序。
I am successfully registering users with the appropriate username, hash and salt properties.我正在使用适当的用户名 hash 和 salt 属性成功注册用户。 However, when attempting to log in, I am always redirected to the failureRedirect route (even when I know that I am getting the usernames and passwords correct).
但是,在尝试登录时,我总是被重定向到 failureRedirect 路由(即使我知道我的用户名和密码是正确的)。
I've gone through StackOverflow and my code with a fine comb but I am just not seeing what is wrong.我已经仔细阅读了 StackOverflow 和我的代码,但我只是没有看到有什么问题。
Below is the configuration on my app.js以下是我的 app.js 上的配置
const express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
LocalStrategy = require('passport-local'),
User = require('./models/user');
//CONFIGURATION OF mongoose, bodyParser, ejs and setting the use of public folders for CSS;
mongoose.connect("mongodb://localhost/kinetic", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
//PASSPORT CONFIG
app.use(require("express-session")({
secret: "Any message can go here apparently",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate())); //comes from passport-local-mongoose
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
Below is a copy of my login form (which I am getting correctly with my get request):以下是我的登录表单的副本(我的获取请求正确):
<form action="/login" method="POST">
<input type="text" name="username">
<input type="password" name="password">
<input type="submit" value="Login">
</form>
As you can see, I am passing in the appropriate name properties for passport.如您所见,我正在传递护照的适当名称属性。
Below is a copy of my post request using passport's middleware:以下是我使用护照中间件的发布请求的副本:
router.post("/login", passport.authenticate("local", {
successRedirect: "/campgrounds",
failureRedirect: "/loginfail"
}), (req, res)=>{
});
Below is a copy of the json of a registered user:下面是一个注册用户的 json 的副本:
{
followers: [],
_id: 5e978583865a1124c8a61d46,
username: 'bob',
bio: 'bob',
salt: 'f9e38abf7383aa431f42bf3a30ecdb037c5876f15c44ff64e961e5759bcbae54',
hash: '379702439bf8e1.........dd2fc7e4f50',
__v: 0
}
EDIT: ./models/user编辑:./models/user
const mongoose = require('mongoose'),
passportLocalMongoose = require('passport-local-mongoose');
const UserSchema = new mongoose.Schema({
username: {type: String, unique: true},
password: String,
});
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
I am completely stumped, although I can imagine that the solution is just under my nose.我完全被难住了,尽管我可以想象解决方案就在我的眼皮底下。
Soo... I think I may have found out what was wrong: Soo...我想我可能已经发现出了什么问题:
In my main file (app.js), I configured my routes at the top of the page (I didn't show my whole file because I never thought it would be necessary (I am new to coding, if you never guessed).在我的主文件 (app.js) 中,我在页面顶部配置了我的路线(我没有显示我的整个文件,因为我从没想过它是必要的(如果你从未猜到的话,我是编码新手)。
My old app.js looked something like this:我的旧app.js 看起来像这样:
const express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
passportLocalMongoose = require('passport-local-mongoose'),
LocalStrategy = require('passport-local'),
User = require('./models/user');
//REQUIRE ROUTE FILES:
const exerciseRoutes = require('./routes/exercises'),
commentRoutes = require('./routes/comments'),
indexRoutes = require('./routes/index');
//TAKE NOTE OVER HERE:
app.use('/', indexRoutes);
app.use('/exercises', exerciseRoutes);
app.use('/exercises/:slug/comments', commentRoutes);
//CONFIGURATION OF mongoose, bodyParser, ejs and setting the use of public folders for CSS;
mongoose.connect("mongodb://localhost/kinetic", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
//PASSPORT CONFIG
app.use(require("express-session")({
secret: "Any message can go here apparently",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate())); //comes from passport-local-mongoose
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get('/', (req, res)=>{
res.send("HOME PAGE!")
});
app.use((req, res, next)=>{
res.locals.currentUser = req.user;
next()
});
//SET UP PORT:
app.listen(3000, ()=>{
console.log("Connected to kinetic!")
})
All I needed to do was move this...我需要做的就是移动这个...
app.use('/', indexRoutes);
app.use('/exercises', exerciseRoutes);
app.use('/exercises/:slug/comments', commentRoutes);
... just above my port/app listener and voilà ...就在我的端口/应用程序侦听器上方,瞧
Now my app.js file looks like this:现在我的 app.js 文件如下所示:
const express = require('express'),
app = express(),
mongoose = require('mongoose'),
bodyParser = require('body-parser'),
passport = require('passport'),
passportLocalMongoose = require('passport-local-mongoose'),
LocalStrategy = require('passport-local'),
User = require('./models/user');
//REQUIRE ROUTE FILES:
const exerciseRoutes = require('./routes/exercises'),
commentRoutes = require('./routes/comments'),
indexRoutes = require('./routes/index');
//CONFIGURATION OF mongoose, bodyParser, ejs and setting the use of public folders for CSS;
mongoose.connect("mongodb://localhost/kinetic", {useUnifiedTopology: true, useNewUrlParser: true, useCreateIndex: true });
app.use(bodyParser.urlencoded({extended:true}));
app.set('view engine', 'ejs');
app.use(express.static(__dirname + '/public'));
//PASSPORT CONFIG
app.use(require("express-session")({
secret: "Any message can go here apparently",
resave: false,
saveUninitialized: false
}));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate())); //comes from passport-local-mongoose
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.get('/', (req, res)=>{
res.send("HOME PAGE!")
});
app.use((req, res, next)=>{
res.locals.currentUser = req.user;
next()
});
//MUST BE BELOW PASSPORT CONFIGURATION!
app.use('/', indexRoutes);
app.use('/exercises', exerciseRoutes);
app.use('/exercises/:slug/comments', commentRoutes);
//SET UP PORT:
app.listen(3000, ()=>{
console.log("Connected to kinetic!")
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.