Auth0 is a flexible, cloud-based identity platform for developers, allowing them to easily add authentication and authorization services to their applications, eliminating friction and the complexity of APIs and custom development.


Read along to see how you can easily enable Single Sign On for your LearnWorlds school using Auth0.


1 Request access to LearnWorlds Restful API.

If you don’t already have access to our APIs go to My accountPremium Members. And click on the button Request API keys as shown below (available in Learning Center plans and above).


This will trigger a request to our support team and within 1-2 working days your API access will be enabled. Upon that you will receive an email from us informing you of the enabling of the service along with usage examples and credentials including your API endpoint, LearnWorlds Client ID and Client Access Token. Store those two somewhere as you will need them for setting up your Auth0 application.

2Create an Auth0 application.

2.1Access your Auth0 tenant management panel. And visit the applications tab and click on the Create Application button.


2.2In the Create Application modal that will appear enter your application name and select Regular Web Application as the application type and then click Create

2.3Access the Application Settings from the tab.

2.4Set the following variables to your settings:

Application Login URI:

https://www.YOURSCHOOL.com/?msg=not-logged-in 

Allowed Callback URLs:

https://www.YOURSCHOOL.comhttps://www.YOURSCHOOL.com/start

Allowed Logout URLs:

https://www.YOURSCHOOL.comhttps://www.YOURSCHOOL.com/start

Allowed Web Origins:

https://www.YOURSCHOOL.comhttps://SCHOOLAPI.com

Allowed Origins (CORS):

https://www.YOURSCHOOL.comhttps://SCHOOLAPI.com

Use the school API url as provided by the LearnWorlds Support team in the email of step1.

2.5Customize your application. Setup your applications customization options and addons based on your needs. You can follow Auth0's documentation to fully customize every aspect of your users authorization experience.

Setting up different connections:

https://auth0.com/docs/connections

Branding and Customization:

https://auth0.com/docs/brand-and-customize

3Create a new Rule.

3.1Visit the Rules page from the sidebar.

3.2Set settings variables.

Key: lwClientId

Value: The LearnWorlds Client id you received from us

Key: lwApi

Value: The LearnWorlds API endpoint you received from us

KeylwClientToken

Value: The LearnWorlds Client Token you received from us

Key: lwSchoolUri

Value: Your School's domain name ex. https://www.myschool.com

3.3Click on the Create Rule button to create a new Rule and then select the Empty Rule option as shown below

    

3.4Copy paste the following code snippet and click Save Changes

function (user, context, callback) {

  const axios = require('axios@0.19.2');

  user.user_metadata = user.user_metadata || {};

  

  const learnWorldsId = user.user_metadata.lwUserId;

  

  const ssoData = {

    username: user.nickname,

    avatar: user.avatar,

    email: user.email,

    redirectUrl: `${configuration.lwSchoolUri}/start`

  };

 

  if (learnWorldsId) {

    ssoData.user_id = learnWorldsId;

  }


  const options = { method: 'POST',

    url: `${configuration.lwApi}/sso`,

    headers: { 

      'content-type': 'application/json',

      'Lw-Client': configuration.lwClientId,

      'Authorization': `Bearer ${configuration.lwClientToken}`

    },

    data: JSON.stringify(ssoData) };


  axios(options)

    .then( (res) => {

      if (res.data.success) {

        user.user_metadata.lwUserId = res.data.user_id;

        auth0.users.updateUserMetadata(user.user_id, user.user_metadata)

        .then(function(){

          context.redirect = {

            url: res.data.url

          };

          return callback(null, user, context);

        })

        .catch(function(err){

          callback(err);

        });

      }

      else {

        console.error('Could not authorize you to learnworlds.');

      }

    })

    .catch( err => {

      console.error('Could not authorize you to learnworlds');

    });  

}

3.5Make sure that the Rule you just created is prioritized as the last one in case you have more rules for your authorization flow.

4Get In touch with us!

Contact our support team and provide them with your Auth0 application id, tenant id and location as shown below. Then you'll be informed as soon as the integration gets enabled from us.