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

相關問題