This page will explain how you can let a user log in to AdultWork.com and receive an access_token to perform an API call and log the user in your application.
Implementing our SDK is easy. Include the Javascript SDK on your page once, ideally right after the <body> openingtag.
<script>(function (d, s, id) { var js, ajs = d.getElementsByTagName(s)[0]; if (d.getElementById(id)) { return; } js = d.createElement(s); js.id = id; js.src = '//cdn.adultwork.com/platform/sdk/sdk.js?v=1.4#APIKey={your-api-key}&clientId={your-clientid}'; ajs.parentNode.insertBefore(js, ajs); }(document, 'script', 'adultwork-jssdk'));</script>
Once the reference is made with our sdk as described in Implement our SDK you can call the "Login" function quite easy.
First initialize the AW.Login sdk module.
Secondly create the callback function to receive the data.
The AW.Login.login_window function accepts 2 functions, the first will be called on a successful login and the second when the pop-up closes. The last will only be called when the user closes the pop-up or clicks cancel.
The last part is to create a button and call the callback function.
<script> // Initialize the login AW.Login.init('{your-clientid}', 'https://platform.adultwork.com/OAuth/Authorize', '{your-returnuri}', '{requested-scopes}', 'token', '{language}', '{button-type}'); function openLogin() { AW.Login.login_window(function (data) { verifyCredentials(data.access_token); },function (){ // optional on close callback function. });
} </script> <input type="button" value="" style="border:0;background:url(//cdn.adultwork.com/Platform/Images/Plugins/LoginButton/btnUL_connect_M.png) no-repeat; width:126px; height:56px;" onclick="openLogin();" />
When the user has logged in correctly and accepted your requested scopes the Unified Login will redirect the user to the redirect_uri supplied in the project details page and send in the returnuri of the initialize function.
Note: The Redirect URI in the project's details page under the Unified Login tab must be equal to the one you supply in the initialize.
Just put the following on that page.
<script src="//cdn.adultwork.com/platform/sdk/sdk.js?v=1.4"> </script> <script> AW.Login.receiveToken();</script>
The receiveToken function will extract the data and send it back to the function which you've defined in step 2 Create a client side function to call the Unified Login. In our example 'openLogin'.
In the example above we receives the access token and call the client side function 'verifyCredentials'. The following example will show the content of this function. For learning purposes we will redirect the user to your /user/login page with the UserID and Nickname of the user in the querystrings. These will be available if we request those scopes in the AW.Login.init function. Please see verifycredentials for more information.
<script> function verifyCredentials(access_token){ var data = null; var xhr = new XMLHttpRequest(); xhr.withCredentials = true; xhr.addEventListener("readystatechange", function () { if (this.readyState === 4) { var userData = JSON.parse(this.responseText); window.location = "/user/login?UserID=" + userData.UserID + "&Nickname=" + userData.Nickname; } }); xhr.open("GET", "https://api.adultwork.com/v1/account/verifycredentials");
xhr.setRequestHeader("accept", "application/json"); xhr.setRequestHeader("authorization", "bearer " + access_token); xhr.setRequestHeader("cache-control", "no-cache"); xhr.send(data); } </script>