How to update loggedInUser after onlogin in Mozilla Persona

I am using Mozilla Persona in a project. I would like to update loggedInUser after onlogin . But loggedInUser is an attribute of the object passed to navigator.id.watch() . navigator.id.watch() was called once (in AngularJS service). Should I repeat it by passing the complete object? This does not seem right. Am I mistaken? = 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(); } }; }); 
+6
source share
2 answers

Can't you make loggedInUser global or at least โ€œlocally globalโ€ at the same scale as your navigator.id.watch method, like an MDN example?

After that, you can get a JSON response from Persona, which contains some data, including email. So you can pass this data in response to AJAX and populate 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); } }); } }); 

Sample JSON response from MDN:

 { "status": "okay", "email": " bob@eyedee.me ", "audience": "https://example.com:443", "expires": 1308859352261, "issuer": "eyedee.me" } 
+3
source

When calling navigator.id.watch set loggedInUser: localStorage.getItem('persona') || null loggedInUser: localStorage.getItem('persona') || null ( loggedInUser: localStorage.getItem('persona') || null is important), then when the user login is successful, execute localStorage.setItem('persona', theUserEmail) when it is not executed, execute localStorage.removeItem('persona') .

0
source

All Articles