JavaScript¶
The authomatic.js library is here to make your life even easier.
It has a very tiny interface similar to it’s Python sibling.
Yo can use it to:
- Process the login procedure in a popup window.
- Access protected resources in the most efficient way without any hassle.
Warning
The library is dependent on jQuery!
-
authomatic.setup(options)¶ Sets up all the library’s options.
Arguments: - options (object) –
An object of following options:
-
authomatic.options.logging¶ boolIftruethe library will log a lot of information to the console.
Popup options:
-
options.popupWidth¶ intThe width of the popup in pixels. Default is800.
-
options.popupHeight¶ intThe height of the popup in pixels. Default is600.
-
options.popupLinkSelector¶ stringA jQuery selector specifying links that should be affected byauthomatic.popupInit(). Default is'a.authomatic'.
-
options.popupFormSelector¶ stringA jQuery selector specifying forms that should be affected byauthomatic.popupInit(). Default is'form.authomatic'.
-
options.popupFormValidator¶ functionA function which you can use to validate the form before the popup opens. It accepts the jQuery selected form. The popup gets opened only if it returnstrue.
Callbacks:
-
options.onPopupInvalid¶ functionCalled when the popup form doesn’t pass validation. Accepts the form selected with jQuery as the only argument.
-
options.onPopupOpen¶ functionCalled when the popup gets open. Accepts the popup location URL as the only argument.
-
options.onLoginComplete¶ functionCalled when the popup gets closed after the login procedure is complete. Accepts theloginResultobject as the only argument.
-
- options (object) –
-
authomatic.popupInit()¶ If you call this function, all
<a></a>and<form></form>elements withclass="authomatic"will open a popup on click/submit. By links the location of the popup will be the value ofhrefattribute, by forms the value ofactionattribute with query string extracted from the form inputs.<!DOCTYPE html> <html> <head> <title>Login Popup Example<title> <!-- authomatic.js is dependent on jQuery --> <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <script type="text/javascript" src="authomatic.js"></script> </head> <body> <!-- Opens a popup with location = "login/facebook" --> <a class="authomatic" href="login/facebook">Login with Facebook</a> <!-- Opens a popup with location = "login/openid?id=me.yahoo.com" --> <form class="authomatic" action="login/openid" method="GET"> <input type="text" name="id" value="me.yahoo.com" /> <input type="submit" value="Login with OpenID" /> </form> <script type="text/javascript"> authomatic.popupInit(); </script> </body> </html>
-
authomatic.access(credentials, url[, options])¶ Makes an asynchronous request to protected resource of a user.
Under the hood it tries to make the request as efficiently as possible with the aim to save your backend’s resources:
- By OAuth 2.0 providers:
- First a cross-domain XHR request is attempted.
- If that fails it continues either with:
- A JSONP XHR request but only if the provider supports it and the request method is
'GET' - Otherwise it will fetch the provider through backend.
- A JSONP XHR request but only if the provider supports it and the request method is
- By OAuth 1.0a providers the request must be signed using the consumer secret which cannot
be exposed in the client, so every request goes first to the backend.
Depending on provider the backend either:
- Fetches the provider and returns the result of the fetch.
- Returns signed request elements with which a JSONP XHR request is made.
Arguments: - credentials (string) – Serialized
Credentialsof the user. - url (string) – URL of the protected resource. Can include querystring and template tags in the form of
https://example.com/api/{user.id}/profile. - options (object) –
An object of following options.
Note
You can also specify all of these options in the
authomatic.setup(). Values specified here will override the values specified inauthomatic.setup()with the exception of callbacks.-
authomatic.options.backend¶ stringURL of your login handler, or the handler where you call theAuthomatic.backend()function.Warning
This parameter is required by all OAuth 1.0a providers and also by some OAuth 2.0 providers.
-
options.forceBackend¶ boolIf true requests will be fetched through backend by all providers.
-
options.substitute¶ objectAn object which will be used to replace template tags inside theURL. e.g. URLhttps://example.com/api/{user.id}/profileby substitute{user: {id: '123'}}will be rendered ashttps://example.com/api/123/profile.
-
options.params¶ objectThe query parameters of the request.
-
options.headers¶ objectThe HTTP headers of the request.
-
options.body¶ stringThe body of the request.
-
options.jsonpCallbackPrefix¶ stringSome providers don’t support cross-domain requests. In such case the function tries a JSONP request and will generate a temporary callback in the global namespace with the name'authomaticJsonpCallback#'where#is an integer unique for every callback. You can change the default'authomaticJsonpCallback'to whatever you want by specifying it in this option.
Callbacks:
Warning
Callbacks specified in
authomatic.setup()will not be overridden by those specified here, but both will be called, whereas those specified inauthomatic.setup()will be called first.-
options.onBackendStart¶ functionCalled whenauthomatic.access()contacts backend. Accepts a object of parameters which will be sent to the backend as the only argument.
-
options.onBackendComplete¶ functionCalled when response returns from backend. Acceptsdata,textStatusandjqXHRas arguments in the specified order.
-
options.onAccessSuccess¶ functionCalled when a successful response returns from provider. Acceptsdata,textStatusandjqXHRas arguments in the specified order.
-
options.onAccessComplete¶ functionCalled when any response returns from provider. AcceptstextStatusandjqXHRas arguments in the specified order.
-
- By OAuth 2.0 providers: