重定向到previous页面验证后使用Node.js的passport.js [英] Redirecting to previous page after authentication in node.js using passport.js
问题描述
我试图建立使用Node.js的,当然preSS和passport.js登录机制。登录本身的工作原理相当不错,还会话使用Redis的存储很好,但我确实有一些麻烦与将用户重定向到他从之前被提示进行身份验证启动。
例如。用户如下链接的http://本地主机:3000 /隐藏
然后重定向到的http://本地主机:3000 /登录
但我希望他能再次回到被重定向到的http://本地主机:3000 /隐藏
这样做的目的是,如果用户访问随机他需要一个页面是在第一次登录,他将被重定向到/登录网站,提供了凭据,然后被重定向回他previously试图网站访问。
下面是我登陆后
app.post('/登录,功能(REQ,资源,下一个){
passport.authenticate('当地',函数(ERR,用户信息){
如果(ERR){
返回下一个(ERR)
}否则如果(!用户){
的console.log('消息:'+ info.message);
返回res.redirect('/登录)
}其他{
req.logIn(用户,功能(错误){
如果(ERR){
返回下一个(ERR);
}
返回下一个(); //<? - 这是行权?
});
}
})(REQ,资源,下一个);
});
在这里我ensureAuthenticated法
函数ensureAuthenticated(REQ,资源,下一个){
如果(req.isAuthenticated()){
返回下一个();
}
res.redirect('/登录');
}
这挂接到 /隐藏
页
app.get('/隐藏,ensureAuthenticated,功能(REQ,RES){
res.render('隐藏',{标题:隐藏的页面'});
});
HTML输出为登录的网站是相当简单
<形式方法=邮报行动=/登录> < DIV ID =用户名>
<标签>用户名:其中; /标签>
<输入类型=文本值=鲍勃NAME =用户名>
< / DIV> < DIV ID =密码>
<标签>密码:LT; /标签>
<输入类型=密码值=秘密NAME =密码>
< / DIV> < DIV ID =信息>< / DIV>
< DIV ID =提交>
<输入类型=提交值=提交>
< / DIV>< /表及GT;
我不知道护照,但这里就是我如何做到这一点:
我有一个中间件我 app.get使用('/帐户,auth.restrict,routes.account)
,设置 redirectTo
会话...然后我重定向到/登录
auth.restrict =功能(REQ,资源,下一个){
如果(!req.session.userid){
req.session.redirectTo ='/帐户;
res.redirect('/登录');
}其他{
下一个();
}
};
然后在 routes.login.post
我做到以下几点:
VAR redirectTo = req.session.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 ? req.session.redirectTo : '/';
delete req.session.redirectTo;
// is authenticated ?
res.redirect(redirectTo);
这篇关于重定向到previous页面验证后使用Node.js的passport.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!