2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-07-21 09:38:31 +02:00
socketio-jwt/example/socketsio-auth0-sample/index.js

42 lines
1.0 KiB
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
};
var port = process.env.PORT || 3001;
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(port, function(){
console.log('listening on *:' + port);
2015-11-26 00:55:27 +01:00
});
2015-12-01 21:03:06 +01:00