问题描述
我正在使用.ejs
作为前端使用node.js, mongoose, express & passport
构建一个RESTful应用程序.我正在尝试使用护照重新添加身份验证.使用当前代码,我可以注册一个新用户,但是该新用户将不会保持登录状态,并且在创建该用户之后,我将无法登录以执行需要授权的活动.似乎没有持久性用户.我也在使用express-session
.
I am building out a RESTful app using node.js, mongoose, express & passport
using .ejs
as a front end. I'm trying to add authentication back in with passport. With the current code I can register a new user, but that new user does not remain logged in, and after creating the user, I cannot login to do activities requiring authorization. There is no persistent user it seems. I'm using express-session
as well.
我已经搜索了类似的问题,但是任何修复"似乎都无济于事.在具有相同堆栈的相似应用中,我有几乎相同的代码...主要区别是资源的名称(例如,种族而不是项目)
I've searched for similar issues but any "fixes" have not seemed to help. I have nearly identical code in a similar app with the same stack...the main difference being the names of the resources (ie races instead of projects for example)
这将是我使用过护照的第三个应用程序,因此它不是100%全新的,并且具有类似/相同的代码,它已在我的本地安装中使用.在这种情况下,它可以正常工作,包括在我的.ejs
文件中显示和隐藏登录/注销按钮.
This will be the 3rd application I've used passport with, so it's not 100% new and with similar / same code, it has worked on my local installation. in this case, it was working fine including showing and hiding the login/logout buttons in my .ejs
file.
然后我重构了路线,现在我根本没有护照可以工作.我已经比较了代码,将身份验证路由放回到app.js
文件中,尝试在有和没有登录的情况下console.log req.user,但它不再起作用了...似乎没有任何护照路由起作用.
Then I refactored the routes and now I cannot get passport to work at all.I have compared code, placed the auth routes back into the app.js
file, tried to console.log the req.user with and without logging in and it just won't work anymore...none of the passport routes seem to function.
我已经从我的package.json
文件中重新安装了节点模块,并复制了&从以前的工作文件中粘贴了护照设置,并且req.user
始终是未定义的,并且我无法注册新用户.
I've reinstalled the node modules from my package.json
file, copied & pasted the passport setup from previously working files, and the req.user
is always undefined, and I'm unable to register a new user.
我基本上已经重新安装了所有通行证,没有删除auth文件和app.js行并重新启动...但是我应该能够在不删除内容的情况下对此进行麻烦.
I've essentially reinstalled all of the passport stuff, short of deleting the auth file and app.js lines and restarting...but I should be able to trouble shoot this without deleting content.
可悲的是,在重构之前,我没有保存版本. :(
Sadly I did not save a version prior to refactoring. :(
关于为什么在重构路由文件后,为什么会发生这种情况的任何建议呢?据我所知,我已经重构了重构之前的东西.
Any suggestions on why this may have occurred AFTER I refactored the routes files when it was working well just prior? As far as I can tell I have reconstructed things as they were prior to refactoring.
这是我的app.js
护照设置
//setup passport
app.use(require("express-session")({
secret: "Fig is the best puppy ever",
resave: false,
saveUninitialized: false
}))
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(function(req, res, next){
console.log("Current User: " + req.user)
res.locals.currentUser = req.user;
next();
});
和我的身份验证路由:
//Authentication Routes
//show signup form
app.get('/register', function(req,res){
res.render("register")
})
//user signup
app.post("/register", function(req,res){
//res.send("Signing you up")
console.log(req.body)
var newUser = new User({username: req.body.username})
User.register(newUser, req.body.password, function(err, user){
if(err) {
console.log(err);
return res.render('register')
} else {
passport.authenticate("local")(req,res, function(){
console.log("Created new user")
res.redirect("/projects")
})
}
})
})
//LOGIN ROUTES
//render login form
app.get("/login", function(req,res){
res.render("login")
})
app.post("/login", passport.authenticate("local",
{
successRedirect: "/projects",
failureRedirect: "/login"
}), function(req,res) {
// console.log("Logged in" +currentUser)
// res.redirect("/projects")
})
//Logout Routes
app.get("/logout", function(req,res) {
req.logout()
res.redirect("/projects")
})
推荐答案
似乎您缺少对authenticate的req.login
调用.尝试将其添加到注册中间件的else
中.
It looks like you're missing the req.login
call on authenticate. Try adding this to your else
in your register middleware.
passport.authenticate("local")(req, res, function() {
req.login(user, function(err) {
if (err) {
console.log(err);
res.redirect('/register');
} else {
res.redirect('/projects')
}
})
})
这篇关于重构代码后无法通过护照进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!