Javascript 如何获取令牌并将其保存,以便以后无需每次登录?
如何在下面的代码中获取Oauth令牌Javascript 如何获取令牌并将其保存,以便以后无需每次登录?,javascript,azure,oauth,azure-mobile-services,Javascript,Azure,Oauth,Azure Mobile Services,如何在下面的代码中获取Oauth令牌 var mobileServiceClient = new WindowsAzure.MobileServices.MobileServiceClient('https://...azurewebsites.net'); mobileService.login("facebook", $scope.token).done(function (results) { $scope.token = result.mobileServiceAuthentic
var mobileServiceClient = new WindowsAzure.MobileServices.MobileServiceClient('https://...azurewebsites.net');
mobileService.login("facebook", $scope.token).done(function (results) {
$scope.token = result.mobileServiceAuthenticationToken;
//??? how to get the first/renewed token?
}
再次打开登录页面时出现以下错误
Error: 'token' is expected to be a value of type object, not string.
at Object.c.createError (azure-mobile-apps-client.min.js:17)
at azure-mobile-apps-client.min.js:17
at new b (azure-mobile-apps-client.min.js:17)
at d.login (azure-mobile-apps-client.min.js:17)
at ChildScope.$scope.signWithFackBook (controllers.js:10)
at fn (eval at compile (ionic.bundle.js:27643), :4:239)
at ionic.bundle.js:65429
at ChildScope.$eval (ionic.bundle.js:30400)
at ChildScope.$apply (ionic.bundle.js:30500)
at HTMLButtonElement. (ionic.bundle.js:65428)
基于,需要将login()
函数中的可选参数token
作为特定对象提供,而不是作为带有现有OAuth令牌的字符串提供。这就引出了你的问题
错误:“令牌”应为object类型的值,而不是string类型的值
实际上,您可以使用以下命令:$rootScope.client.currentUser.mobileServiceAuthenticationToken
获取经过身份验证的令牌
.controller('LoginCtrl', function ($scope, $rootScope, $state) {
if ($rootScope.client.currentUser && $rootScope.client.currentUser.mobileServiceAuthenticationToken) {
$state.go('tab.events');
}
$scope.signWithFackBook = function () {
$rootScope.client.login('facebook').done(function (result) {
$state.go('tab.events');
}, function (error) {
console.error(error);
alert('Failed to login!');
});
}
})
我将在Cordova/ionic中实际使用它,我是否应该在本地存储中保存$rootScope.client.currentUser.mobileServiceAuthenticationToken?或者它会自动持久吗?如果令牌过期了怎么办?请阅读我的“Azure移动30天”,在-它涵盖刷新令牌。是的,您可以将令牌保留在本地存储中,但它可能会过期,因此请确保您准备好了一个刷新令牌,并在应用程序重新启动时立即刷新它。@aaronChen,我在Chrome中尝试了
ionic server
和$rootScope.client之后。currentUser
始终为空。@AdrianHall谢谢。我将阅读该页面以了解如何持久化和刷新令牌。基本上,就像很多应用程序一样,我希望一旦用户在手机上使用facebook登录应用程序,用户就不需要再次登录应用程序。这篇文章是否涵盖了这个功能?
.controller('LoginCtrl', function ($scope, $rootScope, $state) {
if ($rootScope.client.currentUser && $rootScope.client.currentUser.mobileServiceAuthenticationToken) {
$state.go('tab.events');
}
$scope.signWithFackBook = function () {
$rootScope.client.login('facebook').done(function (result) {
$state.go('tab.events');
}, function (error) {
console.error(error);
alert('Failed to login!');
});
}
})