11var app = require ( 'express' ) ( ) ;
2- var http = require ( 'http' ) . Server ( app ) ;
2+ var bodyParser = require ( 'body-parser' ) ;
3+ var http = require ( 'http' ) . createServer ( app ) ;
34var io = require ( 'socket.io' ) ( http ) ;
5+ var jwt = require ( 'jsonwebtoken' ) ;
6+ const config = require ( './config' )
47
58var queue = [ ] ;
69var incorrectTeams = [ ] ;
@@ -10,14 +13,39 @@ function getTeamById(id) {
1013 return teams . indexOf ( id ) + 1 ;
1114}
1215
16+ app . use ( bodyParser . json ( ) )
17+ app . use ( bodyParser . urlencoded ( { extended : true } ) )
18+
1319app . get ( '/' , function ( req , res ) {
1420 res . sendFile ( __dirname + '/index.html' ) ;
1521} ) ;
1622
23+ app . get ( '/queue' , function ( req , res ) {
24+ res . send ( queue )
25+ } ) ;
26+
1727app . get ( '/admin' , function ( req , res ) {
1828 res . sendFile ( __dirname + '/admin.html' ) ;
1929} ) ;
2030
31+ app . post ( '/adminAuth' , function ( req , res ) {
32+ var password = req . body . password ;
33+ if ( password == config . ADMIN_PASSWORD ) {
34+ var token = jwt . sign ( { } , config . PRIVATE_KEY , {
35+ expiresIn : '5h' ,
36+ subject : 'admin'
37+ } ) ;
38+ res . send ( token )
39+ } else res . status ( 401 ) . send ( )
40+ } )
41+
42+ io . of ( '/admin' ) . use ( function ( socket , next ) {
43+ var token = socket . request . _query . token ;
44+ if ( jwt . verify ( token , config . PRIVATE_KEY , { subject : 'admin' } ) )
45+ next ( ) ;
46+ else next ( new Error ( "not authorized" ) )
47+ } ) ;
48+
2149io . of ( '/admin' ) . on ( 'connection' , function ( socket ) {
2250 socket . emit ( 'queue' , queue )
2351 socket . on ( 'correctAnswer' , function ( ) {
0 commit comments