browserid.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. /* This Source Code Form is subject to the terms of the Mozilla Public
  2. * License, v. 2.0. If a copy of the MPL was not distributed with this
  3. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  4. ;(function($, window) {
  5. 'use strict';
  6. $(function() {
  7. django_browserid.registerWatchHandlers();
  8. // Trigger login whenever a login link is clicked, and redirect the user
  9. // once it succeeds.
  10. $(document).on('click', '.browserid-login', function(e) {
  11. e.preventDefault();
  12. var $link = $(this);
  13. django_browserid.login($link.data('next')).then(function(verifyResult) {
  14. window.location = verifyResult.redirect;
  15. }, function(jqXHR) {
  16. try {
  17. var response = JSON.parse(jqXHR.responseText);
  18. if (response.redirect) {
  19. window.location = response.redirect;
  20. } else {
  21. console.error('Unable to redirect after login failure: No redirect provided.');
  22. }
  23. } catch(err) {
  24. console.error('Unable to redirect after login failure: %o', err);
  25. }
  26. });
  27. });
  28. // Trigger logout whenever a logout link is clicked, and redirect the
  29. // user once it succeeds.
  30. $(document).on('click', '.browserid-logout', function(e) {
  31. e.preventDefault();
  32. var $link = $(this);
  33. django_browserid.logout($link.data('next')).then(function(logoutResult) {
  34. window.location = logoutResult.redirect;
  35. }, function(jqXHR) {
  36. console.error('Unable to redirect after logout failure: No redirect provided.');
  37. });
  38. });
  39. });
  40. })(jQuery, window);