javascript.rst 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. JavaScript API
  2. ==============
  3. Normally, you simply include ``browserid/api.js`` and
  4. ``browserid/browserid.js`` on a page, and buttons generated by the
  5. :ref:`template helpers<template-helpers>` will just work. If, however, you want
  6. more control, you can use the JavaScript API, defined in ``api.js`` directly.
  7. For example, if you wanted to trigger login and show a message when there is
  8. an error:
  9. .. code-block:: js
  10. $('.loginButton').click(function() {
  11. django_browserid.login().then(function(verifyResult) {
  12. window.location = verifyResult.redirect;
  13. }, function(jqXHR) {
  14. window.alert('There was an error logging in, please try again.');
  15. });
  16. });
  17. .. note:: See also ``browserid/browserid.js`` for an example of using the API.
  18. This part of the documentation describes the JavaScript API defined in
  19. ``api.js``.
  20. .. js:data:: django_browserid
  21. Global object containing the JavaScript API for interacting with
  22. django-browserid.
  23. Most functions return `jQuery Deferreds`_ for registering asynchronous
  24. callbacks.
  25. .. _`jQuery Deferreds`: https://api.jquery.com/jQuery.Deferred/
  26. .. js:function:: login([requestArgs])
  27. Retrieve an assertion and use it to log the user into your site.
  28. :param object requestArgs: Options to pass to `navigator.id.request`_.
  29. :returns: Deferred that resolves once the user has been logged in.
  30. .. _`navigator.id.request`: https://developer.mozilla.org/en-US/docs/DOM/navigator.id.request
  31. .. js:function:: logout()
  32. Log the user out of your site.
  33. :returns: Deferred that resolves once the user has been logged out.
  34. .. js:function:: getAssertion([requestArgs])
  35. Retrieve an assertion via BrowserID.
  36. :returns: Deferred that resolves with the assertion once it is retrieved.
  37. .. js:function:: verifyAssertion(assertion)
  38. Verify that the given assertion is valid, and log the user in.
  39. :param string assertion: Assertion to verify.
  40. :returns: Deferred that resolves with the login view response once login
  41. is complete.
  42. .. js:function:: getInfo()
  43. Fetch information from the
  44. :func:`browserid_info <django_browserid.helpers.browserid_info>` tag,
  45. such as the parameters for the Persona popup.
  46. :returns: Object containing the data from the info tag.
  47. .. js:function:: getCsrfToken()
  48. Fetch a CSRF token from the
  49. :attr:`CsrfToken view <django_browserid.views.CsrfToken>` via an AJAX
  50. request.
  51. :returns: Deferred that resolves with the CSRF token.
  52. .. js:function:: registerWatchHandlers([onReady])
  53. Register callbacks with navigator.id.watch that make the API work. This
  54. must be called before calling any other API methods.
  55. :param function onReady: Callback that will be executed after the user
  56. agent is ready to process login requests. This is passed as the
  57. ``onready`` argument to `navigator.id.watch`_
  58. .. _`navigator.id.watch`: https://developer.mozilla.org/docs/Web/API/navigator.id.watch