问题描述
我使用Node.js的针对防爆preSS和连接-auth的认证用户。
这是验证请求/索引:
如果(req.isAuthenticated()){
res.redirect('/仪表盘);
}其他{
res.render('指数',{布局:nonav'});
}
不过,注销并回去后F.E. '/仪表板',我可以看到仪表板。
我如何才能把验证检查每一个要求,以确保有一个有效的用户在任何时候?
更新
我没有与认证的任何问题,一切正常!我需要哪些检查每一个路由/请求的解决方案,如果有一个有效的用户,没有把一个函数或路由实现if语句,作为整个应用程序需要一个有效的用户无妨。防爆preSS-验证 - 示例使用路由的定义,这是接近限制,但有许多航线它可以很容易被人遗忘。
app.all(*,功能(REQ,资源,下一个){
如果(req.isAuthenticated()){
下一个();
}其他{
下一个(新的错误(401)); // 401没有获得许可
}
});
//注意:根据你的前妻preSS的版本,
//你可能需要在这里使用app.error,而
//比app.use。
app.use(函数(ERR,REQ,水库旁){
//只是基本的,应填写到下一个()
//或在所有可能的code路径响应
如果(ERR的instanceof错误){
如果(err.message ==='401'){
res.render('error401');
}
}
});
如果您需要认证的路线之前,哪些没有(如首页,登录等)的路线后,定义所有
路线那么它应该只会影响需要它的路线。另外,您可以使用正则表达式,而不是'*'
,其中包括那些需要经过认证路径子路径或列表。
另一种选择是创建一个函数在任何需要身份验证每条路径包括:
函数IsAuthenticated(REQ,资源,下一个){
如果(req.isAuthenticated()){
下一个();
}其他{
下一个(新的错误(401));
}
}
app.get('/登录,功能(REQ,资源,下一个){
res.render('密码');
});
app.get('/仪表盘,IsAuthenticated,功能(REQ,资源,下一个){
res.render(仪表盘);
});
app.get('/设置,IsAuthenticated,功能(REQ,资源,下一个){
res.render(设置);
});
I'm using node.js with Express and connect-auth to authenticate users.
This is the verification when requesting /index:
if(req.isAuthenticated()) {
res.redirect('/dashboard');
} else {
res.render('index', { layout: 'nonav' });
}
However, after logging out and going back to f.e. '/dashboard', I can see the dashboard.
How can I put the authentication check to every request to make sure there's a valid user at all times?
UpdateI don't have any problems with the authentication, everything works fine! I need a solution which checks every route/request if there's a valid user, without putting a function or if-statement in the route-implementation, as the whole App needs a valid user anyway. The Express-Authentication-Example uses "restrict" in the route-definition, which is close, but with many routes it can easily be forgotten.
app.all('*',function(req,res,next){
if(req.isAuthenticated()){
next();
}else{
next(new Error(401)); // 401 Not Authorized
}
});
// NOTE: depending on your version of express,
// you may need to use app.error here, rather
// than app.use.
app.use(function(err,req,res,next){
// Just basic, should be filled out to next()
// or respond on all possible code paths
if(err instanceof Error){
if(err.message === '401'){
res.render('error401');
}
}
});
If you define the all
route before routes which require authentication and after routes which do not (such as the home page, login, etc) then it should only affect the routes that need it. Alternatively you could use a RegExp instead of '*'
, which would include a subpath or list of paths that require authentication.
Another option would be to create a function to include in each route that requires auth:
function IsAuthenticated(req,res,next){
if(req.isAuthenticated()){
next();
}else{
next(new Error(401));
}
}
app.get('/login',function(req,res,next){
res.render('login');
});
app.get('/dashboard',IsAuthenticated,function(req,res,next){
res.render('dashboard');
});
app.get('/settings',IsAuthenticated,function(req,res,next){
res.render('settings');
});
这篇关于检查身份验证凭据每个Node.js的要求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!