6

I'm using Mozilla Persona on a project. I would like to update loggedInUser after onlogin. But loggedInUser is an attribute of an object passed to navigator.id.watch(). navigator.id.watch() was called once (in a AngularJS service). Should I call it again, passing the full object? It doesn't seem right. Am I wrong? =P

Here is my service:

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();
    }
};
});
slacktracer
  • 6,262
  • 6
  • 28
  • 33
  • 1
    In the `app.factory()` call above, are you able to look up the email address of the currently logged in user and then pass that in? Normally what people do is that they have the backend output the email address of the currently logged-in user so that it can be picked up by every call to `navigator.id.watch()`. – François Marier Mar 06 '13 at 02:07

2 Answers2

3

Can't you just make loggedInUser become global or at least "locally global" below the same scope as your navigator.id.watch method, just like the MDN example?

After that you can get the JSON response from the Persona service, which contains some data, including the e-mail. So you could pass that data on your AJAX response and fill the loggedInUser variable

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); }
    });
  }
});

JSON response sample from MDN:

{
  "status": "okay",
  "email": "bob@eyedee.me",
  "audience": "https://example.com:443",
  "expires": 1308859352261,
  "issuer": "eyedee.me"
}
  • 1
    I had to move on so I am still not sure how to best do it, but it's probably something like your answer @chambs. It's the 'one-page-app' nature of my situation that's confusing me, I guess. I'll come back to it eventually... Thank you! – slacktracer Apr 16 '13 at 00:32
0

At the navigator.id.watch call, set loggedInUser: localStorage.getItem('persona') || null (the null is important), then, when Persona login is successful, do localStorage.setItem('persona', theUserEmail), when it is failed, do localStorage.removeItem('persona').

fiatjaf
  • 11,479
  • 5
  • 56
  • 72