[英]Angularjs, Expressjs and passportjs for authentication checks on server side
I have a Node.js application where I have used Yeoman scaffolding for Angular. 我有一个Node.js应用程序,我在其中使用了Yeoman脚手架进行Angular操作。 I am also using ExpressJS for server side. 我也在服务器端使用ExpressJS。
So basically my app structure is: 所以基本上我的应用程序结构是:
app
-->views
--> more files
server.js
The structure is more heavy than this, but this is to keep it simple. 该结构比这更重,但这是为了使其简单。
Basically I have used PassportJS on the server side for authentication. 基本上,我已经在服务器端使用PassportJS进行身份验证。 The routing is currently being carried out via AngularJS routing parameters. 目前,路由是通过AngularJS路由参数执行的。
I have come across an issue now as I need to carry out authentication by using a middleware method: 我现在遇到一个问题,因为我需要使用中间件方法进行身份验证:
function ensureAuthenticated(req, res, next) {
console.log('authenticate=' + req.isAuthenticated());
if (req.isAuthenticated()) {
return next();
}
res.redirect('/')
}
I have created an app.get
on the server side to check if the user tries to get to the admin page without logging in: 我在服务器端创建了一个app.get
,以检查用户是否尝试不登录而进入管理页面:
app.get('/admin', ensureAuthenticated,
function(req, res){
console.log('get admin' + req.params);
res.sendfile(__dirname + '/app/views/admin/users.html');
});
But it never goes to the method. 但是它永远不会用到方法上。 What am I doing wrong? 我究竟做错了什么?
Any advice would be great. 任何建议都很好。
if (!req.isAuthenticated()) {
return res.send(401, 'User is not authorized');
}
next();
The reason it was not working was because angularjs locationprovider was not set to htmlmode5 when configuring the routes on the client side. 它不起作用的原因是因为在客户端上配置路由时,angularjs locationprovider未设置为htmlmode5。 The following line was added: 添加了以下行:
$locationProvider.html5Mode(true);
This basically removes the '#' from the url. 基本上,这会从网址中删除“#”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.