使用passport.js在node.js中进行身份验证后重定向到上一页 [英] Redirecting to previous page after authentication in node.js using passport.js
问题描述
我正在尝试使用 node.js、express 和passport.js 建立登录机制.登录本身工作得很好,会话也很好地存储在 redis 中,但在提示进行身份验证之前,我确实在将用户重定向到他开始的位置时遇到了一些麻烦.
例如用户跟随链接 http://localhost:3000/hidden
然后被重定向到 http://localhost:3000/login
但我希望他再次被重定向回http://localhost:3000/hidden
.
这样做的目的是,如果用户随机访问一个他需要先登录的页面,他将被重定向到/login 站点并提供他的凭据,然后被重定向回他之前尝试访问的站点.
这是我的登录帖
app.post('/login', function (req, res, next) {护照.认证('本地',功能(错误,用户,信息){如果(错误){返回下一个(错误)} else if (!user) {console.log('message:' + info.message);返回 res.redirect('/登录')} 别的 {req.logIn(用户,功能(错误){如果(错误){返回下一个(错误);}返回下一个();//<-?这条线对吗?});}})(req, res, next);});
这里是我的 ensureAuthenticated 方法
function ensureAuthenticated (req, res, next) {如果(req.isAuthenticated()){返回下一个();}res.redirect('/登录');}
钩入/hidden
页面
app.get('/hidden', ensureAuthenticated, function(req, res){res.render('隐藏', { title: '隐藏页面' });});
登录站点的 html 输出非常简单
<div id="密码"><label>密码:</label><input type="password" value="secret" name="password">
<div id="信息"></div><div id="提交"><输入类型=提交"值=提交">
</表单>
我不知道护照,但我是这样做的:
我有一个与 app.get('/account', auth.restrict, routes.account)
一起使用的中间件,它在会话中设置 redirectTo
...然后我重定向到/login
auth.restrict = function(req, res, next){如果(!req.session.userid){req.session.redirectTo = '/account';res.redirect('/登录');} 别的 {下一个();}};
然后在 routes.login.post
中,我执行以下操作:
var redirectTo = req.session.redirectTo ||'/';删除 req.session.redirectTo;//认证了吗?res.redirect(redirectTo);
I'm trying to establish a login mechanism using node.js, express and passport.js. The Login itself works quite nice, also sessions are stored nicely with redis but I do have some troubles with redirecting the user to where he started from before being prompted to authenticate.
e.g. User follows link http://localhost:3000/hidden
is then redirected to http://localhost:3000/login
but then I want him to be redirected again back to http://localhost:3000/hidden
.
The purpose of this is, if the user access randomly a page he needs to be logged in first, he shall be redirected to the /login site providing his credentials and then being redirected back to the site he previously tried to access.
Here is my login post
app.post('/login', function (req, res, next) {
passport.authenticate('local', function (err, user, info) {
if (err) {
return next(err)
} else if (!user) {
console.log('message: ' + info.message);
return res.redirect('/login')
} else {
req.logIn(user, function (err) {
if (err) {
return next(err);
}
return next(); // <-? Is this line right?
});
}
})(req, res, next);
});
and here my ensureAuthenticated Method
function ensureAuthenticated (req, res, next) {
if (req.isAuthenticated()) {
return next();
}
res.redirect('/login');
}
which hooks into the /hidden
page
app.get('/hidden', ensureAuthenticated, function(req, res){
res.render('hidden', { title: 'hidden page' });
});
The html output for the login site is quite simple
<form method="post" action="/login">
<div id="username">
<label>Username:</label>
<input type="text" value="bob" name="username">
</div>
<div id="password">
<label>Password:</label>
<input type="password" value="secret" name="password">
</div>
<div id="info"></div>
<div id="submit">
<input type="submit" value="submit">
</div>
</form>
I don't know about passport, but here's how I do it:
I have a middleware I use with app.get('/account', auth.restrict, routes.account)
that sets redirectTo
in the session...then I redirect to /login
auth.restrict = function(req, res, next){
if (!req.session.userid) {
req.session.redirectTo = '/account';
res.redirect('/login');
} else {
next();
}
};
Then in routes.login.post
I do the following:
var redirectTo = req.session.redirectTo || '/';
delete req.session.redirectTo;
// is authenticated ?
res.redirect(redirectTo);
这篇关于使用passport.js在node.js中进行身份验证后重定向到上一页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!