6

我在一个项目中使用 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();
    }
};
});
4

2 回答 2

3

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

之后,您可以从 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); }
    });
  }
});

来自 MDN 的 JSON 响应示例:

{
  "status": "okay",
  "email": "bob@eyedee.me",
  "audience": "https://example.com:443",
  "expires": 1308859352261,
  "issuer": "eyedee.me"
}
于 2013-04-02T00:34:12.883 回答
0

navigator.id.watch调用时,设置loggedInUser: localStorage.getItem('persona') || nullnull重要),然后,当 Persona 登录成功时,做localStorage.setItem('persona', theUserEmail),当它失败时,做localStorage.removeItem('persona')

于 2014-02-05T16:25:00.470 回答