persona.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. "use strict";
  2. function init_persona() {
  3. if (!(navigator.id && window.$ && window.url)) {
  4. if (window.console) {
  5. console.log('FreshRSS (Persona) waiting for JS…');
  6. }
  7. window.setTimeout(init_persona, 100);
  8. return;
  9. }
  10. $('a.signin').click(function() {
  11. navigator.id.request();
  12. return false;
  13. });
  14. $('a.signout').click(function() {
  15. navigator.id.logout();
  16. return false;
  17. });
  18. navigator.id.watch({
  19. loggedInUser: context['current_user_mail'],
  20. onlogin: function(assertion) {
  21. // A user has logged in! Here you need to:
  22. // 1. Send the assertion to your backend for verification and to create a session.
  23. // 2. Update your UI.
  24. $.ajax ({
  25. type: 'POST',
  26. url: url['login'],
  27. data: {assertion: assertion},
  28. success: function(res, status, xhr) {
  29. if (res.status === 'failure') {
  30. openNotification(res.reason, 'bad');
  31. } else if (res.status === 'okay') {
  32. location.href = url['index'];
  33. }
  34. },
  35. error: function(res, status, xhr) {
  36. // alert(res);
  37. }
  38. });
  39. },
  40. onlogout: function() {
  41. // A user has logged out! Here you need to:
  42. // Tear down the user's session by redirecting the user or making a call to your backend.
  43. // Also, make sure loggedInUser will get set to null on the next page load.
  44. // (That's a literal JavaScript null. Not false, 0, or undefined. null.)
  45. $.ajax ({
  46. type: 'POST',
  47. url: url['logout'],
  48. success: function(res, status, xhr) {
  49. location.href = url['index'];
  50. },
  51. error: function(res, status, xhr) {
  52. // alert(res);
  53. }
  54. });
  55. }
  56. });
  57. }
  58. if (document.readyState && document.readyState !== 'loading') {
  59. if (window.console) {
  60. console.log('FreshRSS (Persona) immediate init…');
  61. }
  62. init_persona();
  63. } else if (document.addEventListener) {
  64. document.addEventListener('DOMContentLoaded', function () {
  65. if (window.console) {
  66. console.log('FreshRSS (Persona) waiting for DOMContentLoaded…');
  67. }
  68. init_persona();
  69. }, false);
  70. }