title | description | name | alias | language | framework | image | tags | snippets | seo_alias | ||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Auth0 Socket.io SDK Tutorial |
This tutorial will show you how to use the Auth0 Socket.io SDK to add authentication and authorization to your web app. |
Socket.io |
|
|
|
/media/platforms/socketio.svg |
|
|
socket-io |
::: panel-info System Requirements This tutorial and seed project have been tested with the following:
- Socket.io 1.4.5
- NodeJS 5.0.0 :::
<%= include('../_includes/_package', { pkgRepo: 'socketio-jwt', pkgBranch: 'master', pkgPath: 'example/socketsio-auth0-sample', pkgFilePath: null, pkgType: 'server' }) %>
If you have an existing application, follow the steps below.
Go to the Application 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
Install socketio-jwt from npm and save it to your package.json
using
npm install --save socketio-jwt
Add the code below to the index.html
file to include the Auth0 lock
script and set the viewport:
${snippet(meta.snippets.dependencies)}
Configure Auth0Lock with your clientId
and domain
:
${snippet(meta.snippets.setup)}
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:
${snippet(meta.snippets.use)}
To discover all the available arguments for lock.show
, see .show([options, callback]).
Add the following to your index.js
file.
var app = require('express')();
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketioJwt = require('socketio-jwt');
io
.on('connection', socketioJwt.authorize({
secret: Buffer('${account.clientSecret}', 'base64'),
timeout: 15000 // 15 seconds to send the authentication message
})).on('authenticated', function(socket) {
//this socket is authenticated, we are good to handle more events from it.
console.log('hello! ' + JSON.stringify(socket.decoded_token));
});
Note: If you are not using a base64-encoded secret, then you don't need to convert it to a Buffer, so you can use: secret: 'your secret or public key'
.
Add the following snippet before the </body>
on index.html
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io();
socket.on('connect', function () {
socket.on('authenticated', function () {
//Do
})
.emit('authenticate', {token: userToken}); // send the jwt
});
</script>
No URL is specified when doing var socket = io();
, because the default behaviour is to connect to the host that serves the page.
You have completed the implementation of Login and Signup with Auth0 and Socket.io.