This repository has been archived on 2024-11-11. You can view files and clone it, but cannot push or open issues or pull requests.
socketio-jwt/example/socketsio-auth0-sample/index.js

40 lines
1018 B
JavaScript
Raw Normal View History

2015-12-01 21:03:06 +01:00
var express = require('express');
var app = express();
2015-11-26 00:55:27 +01:00
var http = require('http').Server(app);
var io = require('socket.io')(http);
var socketioJwt = require('socketio-jwt');
2015-12-01 21:03:06 +01:00
var dotenv = require('dotenv');
2015-11-26 00:55:27 +01:00
2015-12-01 21:03:06 +01:00
dotenv.load();
2015-11-26 00:55:27 +01:00
2015-12-01 21:03:06 +01:00
var env = {
AUTH0_CLIENT_ID: process.env.AUTH0_CLIENT_ID,
2016-04-27 23:44:50 +02:00
AUTH0_DOMAIN: process.env.AUTH0_DOMAIN
};
2015-12-01 21:03:06 +01:00
2016-04-27 23:44:50 +02:00
app.set('views', __dirname + '/views');
app.set('view engine', 'pug');
2015-11-26 00:55:27 +01:00
io
.on('connection', socketioJwt.authorize({
2015-12-01 21:03:06 +01:00
secret: Buffer(JSON.stringify(process.env.AUTH0_CLIENT_SECRET), 'base64'),
2015-11-26 00:55:27 +01:00
timeout: 15000 // 15 seconds to send the authentication message
}))
.on('authenticated', function(socket){
console.log('connected & authenticated: ' + JSON.stringify(socket.decoded_token));
socket.on('chat message', function(msg){
debugger;
io.emit('chat message', msg);
});
});
2015-12-01 21:03:06 +01:00
app.use(express.static(__dirname + '/public'));
2015-12-01 21:03:06 +01:00
app.get('/', function (req, res) {
2016-04-27 23:44:50 +02:00
res.render('index', { env: env });
});
2015-11-26 00:55:27 +01:00
http.listen(3001, function(){
console.log('listening on *:3001');
});