如何在Mozilla Persona中的onlogin之后更新loggedInUser

sla*_*cer 6 javascript mozilla login browserid angularjs

我在一个项目上使用Mozilla Persona.我想更新loggedInUser之后onlogin.但是loggedInUser传递给对象的属性navigator.id.watch(). navigator.id.watch()被调用一次(在AngularJS服务中).我应该再次调用它,传递整个对象吗?这似乎不对.我错了吗?= P

这是我的服务:

app.factory('persona', function ($rootScope, $http) {
navigator.id.watch({
    loggedInUser: null,
    onlogin: function onlogin(assertion) {
        console.log(this);
        $http.post('/signIn', { assertion: assertion })
            .then(function (data, status, headers, config) {
                $rootScope.$broadcast('signIn', data.data);
            }, function (data, status, headers, config) {
                $rootScope.$broadcast('signInError', data.data);
            });
    },
    onlogout: function onlogout(param) {
        $http.get('/signOut')
            .then(function (data, status, headers, config) {
                $rootScope.$broadcast('signOut', data.data);
            }, function (data, status, headers, config) {
                $rootScope.$broadcast('signOutError', data.data);
            });
    }
});

return {
    signIn: function signIn() {
        navigator.id.request();
    },
    signOut: function signOut() {
        navigator.id.logout();
    }
};
});
Run Code Online (Sandbox Code Playgroud)

小智 3

难道你不能像 MDN 示例一样,使 LoggedInUser 成为全局的,或者至少在与你的 navigator.id.watch 方法相同的范围内成为“本地全局”吗?

之后,您可以从 Persona 服务获取 JSON 响应,其中包含一些数据,包括电子邮件。因此,您可以在 AJAX 响应中传递该数据并填充loggedInUser 变量

https://developer.mozilla.org/en-US/docs/Persona/Quick_Setup#Step_3.3A_Watch_for_login_and_logout_actions

var currentUser = 'bob@example.com';

navigator.id.watch({
  loggedInUser: currentUser,
  onlogin: function(assertion) {
    $.ajax({ 
      type: 'POST',
      url: '/auth/login', // This is a URL on your website.
      data: {assertion: assertion},
      success: function(res, status, xhr) { window.location.reload(); },
      error: function(xhr, status, err) {
        navigator.id.logout();
        alert("Login failure: " + err);
      }
    });
  },
  onlogout: function() {
    $.ajax({
      type: 'POST',
      url: '/auth/logout', // This is a URL on your website.
      success: function(res, status, xhr) { window.location.reload(); },
      error: function(xhr, status, err) { alert("Logout failure: " + err); }
    });
  }
});
Run Code Online (Sandbox Code Playgroud)

来自 MDN 的 JSON 响应示例:

{
  "status": "okay",
  "email": "bob@eyedee.me",
  "audience": "https://example.com:443",
  "expires": 1308859352261,
  "issuer": "eyedee.me"
}
Run Code Online (Sandbox Code Playgroud)