0

我想在用户名和密码是正确的它使用ngStorage返回令牌和存储在本地存储,但我看,它不是固定在localStorage的节能令牌一些文章,和他们做认证通过API,用户参考将其存储到cookie Http。我怎样才能令牌存储装置上的cookie HTTP和为它的安全还是有处理令牌认证基于令牌的认证与角

$scope.loginUser = function(){ 
    $http({ 
     method: 'POST', 
     url: 'ourdomain.com/api/token', 
     headers: 
     { 
      'Content-type': 'application/x-www-form-urlencoded', 
     }, 
     data: 
     'UserName=' + $scope.username + 
     '&Password=' + $scope.password 
     }).success(function(data, status) { 
       $localStorage.token = data; 
       console.log(data); 
     }) 
     .error(function(data, status) { 
      console.log("Error"); 
     }); 
} 
+0

您可以用寻找到的角度开始-cookies:https://github.com/angular/bower-angular-cookies – Rob

回答

1

一个更好的办法,我热烈建议您使用stallizer:https://github.com/sahat/satellizer

登录电子邮件和密码

客户:输入你的电子邮件和密码登录表单。

客户:在表单上提交电子邮件和密码$ auth.login()。

客户端:发送POST请求到/ auth/login。 服务器:检查电子邮件存在,如果不是 - 回报401

服务器:检查密码是否正确,如不 - 回报401

服务器:创建一个JSON网络令牌,并将其发送回客户端。

客户:解析令牌,并将其保存到本地存储页面重载后的后续使用。

var user = { 
    email: $scope.email, 
    password: $scope.password 
}; 

$auth.login(user) 
    .then(function(response) { 
    // Redirect user here after a successful log in. 
    }) 
    .catch(function(response) { 
    // Handle errors here, such as displaying a notification 
    // for invalid email and/or password. 
    }); 


// Controller 
$scope.isAuthenticated = function() { 
    return $auth.isAuthenticated(); 
}; 

<!-- Template --> 
<ul ng-if="!isAuthenticated()"> 
    <li><a href="/login">Login</a></li> 
    <li><a href="/signup">Sign up</a></li> 
</ul> 
<ul ng-if="isAuthenticated()"> 
    <li><a href="/logout">Logout</a></li> 
</ul> 
+0

我可以在我的API中使用这个吗?我可以看到clientID,什么是clientID?此外,我认为它使用其他社交网络登录,但不是与我自己的登录API – user3055606

+0

,你可能需要适应你的后端代码。 – thegio

+0

使我的后端代码适应什么?例如 – user3055606