Socket.io

Community maintained

1. Set up the Allowed Origin (CORS) in Auth0

Go to the Client Settings section in the Auth0 dashboard and make sure to add your URL as an Allowed Origin (CORS). If you're testing it locally, it should contain the following value:

http://localhost:3001

2. Installation

Install socketio-jwt from npm and save it to your package.json using

npm install --save socketio-jwt

3. Add the Auth0 Script and Set the Viewport

Add the code below to the index.html file to include the Auth0 lock script and set the viewport:

<!-- Auth0Lock script -->
<script src="https://cdn.auth0.com/js/lock/10.19/lock.min.js"></script>

<!-- Setting the right viewport -->
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />

4. Configure Auth0Lock

Configure Auth0Lock with your clientId and domain:

var lock = null;
$(document).ready(function() {
   lock = new Auth0Lock('YOUR_CLIENT_ID', 'YOUR_AUTH0_DOMAIN');
});

To discover all the available options, see User configurable options.

5. Implement the Login

To implement the login, call the .show() method of Auth0's lock instance when a user clicks the login button, and save the JWT token to localStorage for later use in calling a server or an API:

var userProfile;
var userToken = localStorage.getItem('userToken');
var accessToken = localStorage.getItem('accessToken');

lock.on('authenticated', function(authResult) {
    lock.getUserInfo(authResult.accessToken, function(error, profile) {
        if (error) {
            // Handle error
            return;
        }
        localStorage.setItem('userToken', authResult.idToken);
        localStorage.setItem('accessToken', authResult.accessToken);
        userProfile = profile;
        userToken = authResult.idToken;
    });
});

if (userToken && accessToken) {
    lock.getUserInfo(accessToken, function (err, profile) {
        if (err) {
            return alert('There was an error getting the profile: ' + err.message);
        }        
        userProfile = profile;
    });
}

$('#login button').click(function(e){
  e.preventDefault();
  lock.show();
});
Use Auth0 for FREECreate free Account