Working Example

Below is a minimalist example showing how to download and interact with the Y8 JS SDK.

If a backend server needs to act on behalf of a user, see the using a redirect URI page. For example responses, see the auth functions page.

<!DOCTYPE html>
<html>
<head>
<title>SDK Example</title>
<script src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
</head>
<body>
<a href="#" id="sdk-login">Login</a>
<a href="#" id="sdk-register">Register</a>
<script>
// Optional callback to handle response data
var loginCallback = function(response) {
  if (response) {
    console.log(response);
    console.log(response.authResponse.details);
  }
}

window.idAsyncInit = function() { // SDK downloaded
  ID.Event.subscribe('id.init', function() { // SDK initialized
    ID.getLoginStatus(function(data) { // Try Autologin
      if (data.status == 'not_linked') {
        ID.login(loginCallback);
      }
    });


    $('#sdk-login').click(function() {
      ID.login(loginCallback);
    });
    $('#sdk-register').click(function() {
      ID.register(loginCallback);
    });
  });

  ID.init({
    appId : 'YOUR APP ID'
  });
};

// Load the JS SDK
(function(d, s, id){
    var js, fjs = d.getElementsByTagName(s)[0];
    if (d.getElementById(id)) {return;}
    js = d.createElement(s); js.id = id;
    js.src =  document.location.protocol == 'https:' ? "https://cdn.y8.com/api/sdk.js" : "http://cdn.y8.com/api/sdk.js";
    fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'id-jssdk'));
</script>
</body>
</html>