javascript - 如何获取token并保存以供以后使用而不需要每次登录?

标签 javascript azure oauth azure-mobile-services

下面的代码如何获取Oauth token?

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)

BTW, I want the page to redirect to the main page if the token is valid.

  .controller('LoginCtrl', function ($scope, $rootScope, $state) {

    if ($scope.token ....is good...) // Need to test if the token is good
    { 
        $state.go('tab.events'); 
    }

    $scope.signWithFackBook = function () {
      $rootScope.client.login('facebook', $scope.token).done(function (result) {
        $scope.token = result.mobileServiceAuthenticationToken;
        $state.go('tab.events');
      }, function (error) {
        console.error(error);
        alert('Failed to login!');
      });
    }
  })

最佳答案

基于the API documentationlogin() 函数中的可选参数 token 需要作为特定对象提供,而不是带有现有 OAuth token 的字符串。这就提出了你的问题。

Error: 'token' is expected to be a value of type object, not string.

实际上,您可以使用以下内容:$rootScope.client.currentUser.mobileServiceAuthenticationToken 来获取经过身份验证的 token 。

.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!');
    });
  }
})

关于javascript - 如何获取token并保存以供以后使用而不需要每次登录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42524293/

相关文章:

javascript - Chrome 扩展破坏了 DOM

c# - 是否可以针对多个身份验证提供程序来保护 ASP.NET Web API 2 应用程序的安全?

azure - 如何编写在添加新用户时触发的 Azure B2C 自定义策略

facebook - 有没有人真正让 ASP.NET MVC 5 Identity Facebook 登录开箱即用? (loginInfo 始终返回 null)

authentication - Haskell 独立桌面应用程序身份验证(使用 google/facebook/twitter/etc 帐户)

javascript - Underscore.js 模板 IE9/IE10 未返回正确的代码

javascript - 如何防止标签内的链接触发复选框?

javascript - 元素单击在 Firefox 和 IE 中不起作用,但在 Chrome 中起作用

c# - EF6 中是否可以使用特定于查询的 DbExecutionStrategies?

javascript - 将 Chrome 扩展连接到 StackExchange oAuth API