Node.js 在Angular和Node中执行简单身份验证
我一直在努力在angular node应用程序中执行简单的身份验证。我很清楚有现成的angular express yeoman套件。但我想充分理解这一点,从而理解这一尝试 到目前为止,我所能做的是创建一个连接到节点服务器的登录表单。它感测登录名和密码并接收回复 我陷入困境的是如何将这个简单的交互转换为身份验证过程 我的目录结构如下Node.js 在Angular和Node中执行简单身份验证,node.js,angularjs,Node.js,Angularjs,我一直在努力在angular node应用程序中执行简单的身份验证。我很清楚有现成的angular express yeoman套件。但我想充分理解这一点,从而理解这一尝试 到目前为止,我所能做的是创建一个连接到节点服务器的登录表单。它感测登录名和密码并接收回复 我陷入困境的是如何将这个简单的交互转换为身份验证过程 我的目录结构如下 --ParentDirectory/ -client/ --css/ --lib/ /*all angular and jquery
--ParentDirectory/
-client/
--css/
--lib/ /*all angular and jquery library files*/
--src/ /* All other angular modules, directives etc */
--app.js
--index.html /* default page associated with app.js
--login.js /*module login is independent of app.js module */
--login.html
-server/
--server.js /*restify code sits here */
app.js是主应用程序所在的位置。
目前看来:
angular.module('app',['']);
angular.module('app').controller('mainCtrl',function($scope){
$scope.hello = "Hello World";
});
现在首先要做的是。。当用户访问我的网站时,即index.html页面。。他们将在这个应用程序结束,我想让他们重新定向到login.html页面,如果他们没有经过身份验证。
关于如何做到这一点有什么线索吗
继续
Login.html只要求输入用户名和密码(此处不显示代码以保持简洁)
Login.js如下所示:
angular.module('loginApp',['common.webservice'])
.controller('loginCtrl',['$scope','WSLogin','$location','$window','Authen',function($scope,WSLogin,$location,$window,Authen){
$scope.message;
$scope.submit = function(){
var temp = {logonID: $scope.username,password: $scope.password};
WSLogin.save(temp,function(result){
Authen.isLogged = true;
$window.sessionStorage.token = result.token;
$scope.message = result.token;
$location.path("/main");
},function(err){
$scope.message = "Authentication failed. Pls retry";
});
};
$scope.logout = function(){
if (AuthenticationService.isLogged){
Authen.isLogged = false;
delete $window.sessionStorage.token;
$location.path("/");
}
}
}])
.factory('Authen', function() {
var auth = { isLogged :false };
return auth;
});
var restify = require('restify');
var server = restify.createServer({
log: log,
name: 'demo'
});
server.listen(12345, function(){
console.log('%s listening at %s', server.name,server.url);
});
server.post('/user/authenticate',function(req,res,next){
if (!(req.params.logonID === "test" && req.params.password === "test")) {
res.send(401, 'Wrong user or password');
return;
}
var profile = {
first_name: 'John',
last_name: 'Doe',
email: 'john@doe.com',
id: 123
};
// we are sending the profile inside the token
res.json({token:profile);
});
WSLogin是一种连接到路径user/authenticate上的节点服务的资源。到目前为止,此Web服务运行良好。
[此处不显示代码以保持简洁]
server.js文件如下所示:
angular.module('loginApp',['common.webservice'])
.controller('loginCtrl',['$scope','WSLogin','$location','$window','Authen',function($scope,WSLogin,$location,$window,Authen){
$scope.message;
$scope.submit = function(){
var temp = {logonID: $scope.username,password: $scope.password};
WSLogin.save(temp,function(result){
Authen.isLogged = true;
$window.sessionStorage.token = result.token;
$scope.message = result.token;
$location.path("/main");
},function(err){
$scope.message = "Authentication failed. Pls retry";
});
};
$scope.logout = function(){
if (AuthenticationService.isLogged){
Authen.isLogged = false;
delete $window.sessionStorage.token;
$location.path("/");
}
}
}])
.factory('Authen', function() {
var auth = { isLogged :false };
return auth;
});
var restify = require('restify');
var server = restify.createServer({
log: log,
name: 'demo'
});
server.listen(12345, function(){
console.log('%s listening at %s', server.name,server.url);
});
server.post('/user/authenticate',function(req,res,next){
if (!(req.params.logonID === "test" && req.params.password === "test")) {
res.send(401, 'Wrong user or password');
return;
}
var profile = {
first_name: 'John',
last_name: 'Doe',
email: 'john@doe.com',
id: 123
};
// we are sending the profile inside the token
res.json({token:profile);
});
我如何修补这些东西,使其有点像身份验证系统。
我从各种博客、网站等获得了一些提示。。但是实现它们并不顺利。我知道您想知道如何构建这些东西,但我强烈建议您在节点/服务器端使用它们进行身份验证。话虽如此,在登录的实现中,在响应中包含带有令牌的cookie可能比在响应中返回令牌更容易。然后,代码可以返回200(或201)响应代码以指示登录成功,但通过将令牌移动到cookie,客户机代码不必处理在将来的请求中发送令牌的问题——它在cookie中自动发送 继续讨论客户端问题,请记住,您将启用安全性来保护服务器端的资源。您在客户端JavaScript代码中输入的任何内容都可以被任何人读取。因此,最终这些返回受保护数据的API才是需要保护的API。同样,Passport提供了一种保护API的简单方法,但是您可以在提供数据之前验证每个API请求是否包含此令牌(或cookie) 如果请求未经授权(它不包含令牌,或者令牌无效),您可以返回401(未经授权)响应。在客户端代码中,您可以对此进行检查,并自动将用户路由到登录页面 使用Angular,实现这一点的一种模式是使用它,它允许您钩住每个HTTP请求。您可以检查状态为401的
响应错误
,并将其发送到登录页面