Login a user

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.

  1. Implement our SDK.
  2. Create a client side function to call the Unified Login.
  3. Receive the access_token and let the sdk return it to the callback function.
  4. Call the verify credentials endpoint to receive the UserID and other data that you've requested.

 

 

Implement our SDK

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>
  • {your-api-key}
    • The api key of your project. This can be found in the project's details page.
  • {your-clientid}
    • The client identifier. This can be found in the project's details page under the Unified Login tab.

 

 

Create a client side function to call the Unified Login.

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();" />
  • {your-clientid}
    • The client identifier. This can be found in the project's details page under the Unified Login tab.
  • {your-returnuri}
    • The uri the Unified Login will redirect to. This can be found in the project's details page under the Unified Login tab.
  • {requested-scopes}
    • The space separated scopes you will need to perform an API call. See scopes for all available scopes. (for example: 'userid nickname profile_about')
  • token
    • The type of access_token. In this scenario token is required.
  • {language}
    • The language of the Unified Login. The default is en-GB. If you choose a language that is not (yet) supported en-GB will be used.
  • {button-type}
    • The type of button. We currently have 3 types of buttons; Login, Register and a combination of both; Connect. This will load up the correct form in the Unified Login.

 

 

Receive the access_token and let the sdk return it to the callback function.

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'.

  • access_token
    • The access token issued by the Unified Login.
  • token_type
    • The Unified Login is using the "bearer" token type.
  • expires_in
    • The lifetime in seconds of the access token. For example, the value "3600" denotes that the access token will expire in one hour from the time the response was generated.
  • state
    • This is a value used by the client to maintain state between the request and callback. The Unified Login includes this value when redirecting the user-agent back to the client.

 

 

 

Call the verify credentials endpoint to receive the UserID and other data that you've requested.

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>