Angularjs 使用sails LocalStrategy获得简单的身份验证
我正在开发一个带有登录函数和其他api的rest服务器(都经过身份验证,并且都启用了cors) 所以我有myhost/api/login(我可以在post中传递电子邮件和密码) 另一方面,我有一些客户端,在我的例子中是angular编写的,我希望他们可以使用该api登录 例如,查看以下客户端代码:Angularjs 使用sails LocalStrategy获得简单的身份验证,angularjs,authentication,cookies,sails.js,restful-authentication,Angularjs,Authentication,Cookies,Sails.js,Restful Authentication,我正在开发一个带有登录函数和其他api的rest服务器(都经过身份验证,并且都启用了cors) 所以我有myhost/api/login(我可以在post中传递电子邮件和密码) 另一方面,我有一些客户端,在我的例子中是angular编写的,我希望他们可以使用该api登录 例如,查看以下客户端代码: (function() { angular.module('controller.auth', []) .controller("authController", ['$q','$scope', '$
(function() {
angular.module('controller.auth', [])
.controller("authController", ['$q','$scope', '$interval', '$http', '$cookies', function( $q, $scope, $interval,$http, $cookies) {
console.log('auth');
$scope.whoami = false;
$scope.login = function() {
$http({
method: 'POST',
url: 'http://api.marketplace.local:1337/login',
headers: {
'Content-Type': 'application/json'
},
data: {email: 'myemail@email.com', password: 'password'}
})
.then(function(res) {
$scope.logged = true;
}, function(res) {
console.log('error');
console.log(res);
});
}
$scope.me = function() {
$http({
method: 'POST',
url: 'http://api.marketplace.local:1337/api/user/me',
headers: {
'Content-Type': 'application/json'
}
})
.then(function(res) {
$scope.me = res.data;
}, function(res) {
console.log('error');
console.log(res);
});
}
}])
})();
这是html:
<div ng-controller="authController">
<div ng-click="login()" style="cursor: pointer">LOGIN</div>
<div ng-show="logged" ng-click="me()" style="cursor: pointer">WHOAMI</div>
<div ng-show="whoami">Hi, I'm {{whoami.email}}</div>
</div>
登录
哇
嗨,我是{{whoami.email}
当我点击LOGIN时,登录成功,但当我点击WHOAMI时,我有一个403
那么,如何使用login函数为下一次调用获得身份验证
我的意思是,我想在登录后从响应中获取cookie,并将其设置为下一次调用,但如果我看到firebug中的cookie,我就无法从$http()访问。然后()
另一个想法是在登录响应中发送sessionId,但我不知道如何使用它
我需要一些建议去哪里找。这里是答案:这里是答案: