From 02b4a0a32043f330453704c5146d2772a83a1f72 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Thu, 27 Dec 2018 00:16:59 +0100 Subject: [PATCH] route logging and login into usercp (game) --- inc/fftcgrouter.coffee | 30 ++++++++++++++++++++++++++++++ server.coffee | 7 ------- src/index.coffee | 1 + 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/inc/fftcgrouter.coffee b/inc/fftcgrouter.coffee index 9b6259c..d068c57 100644 --- a/inc/fftcgrouter.coffee +++ b/inc/fftcgrouter.coffee @@ -10,6 +10,17 @@ fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db') # create router FFTCGROUTER = express.Router() + +# request logging +FFTCGROUTER.use (req, res, next) -> + if req.session.user + console.log "[FFTCG] user '#{req.session.user.login}' requested '#{req.url}'" + else + console.log "[FFTCG] requested '#{req.url}'" + + next() + +# static content FFTCGROUTER.use express.static path.resolve(__dirname, '../public_html') # register user @@ -48,4 +59,23 @@ FFTCGROUTER.post '/login', (req, res) -> status: 'fail' text: err +# Templates +FFTCGROUTER.get '/:template.html', (req, res) -> + if req.session.user and req.params.template == 'index' + return res.redirect '/game.html' + + + # render requested template + res.render (req.params.template + '.pug'), (err, html) -> + # redirect invalid requests to index + if err + return res.redirect '/index.html' + + # actual response + res.send html + +# default route +FFTCGROUTER.use (req, res) -> + return res.redirect '/index.html' + module.exports = FFTCGROUTER diff --git a/server.coffee b/server.coffee index c4e49ef..78e669c 100644 --- a/server.coffee +++ b/server.coffee @@ -24,13 +24,6 @@ app.use sessionMiddleware # routes app.use FFTCGROUTER -# Templates -app.set 'view engine', 'pug' -app.get '/:template.html', (req, res) -> - if req.session.user - console.log "[FFTCG] user is '#{req.session.user.login}'" - res.render (req.params.template + '.pug') - # socket.io web = http.Server app socket = new FFTCGSOCKET web, sharedSession sessionMiddleware diff --git a/src/index.coffee b/src/index.coffee index d0908ab..130bcc4 100644 --- a/src/index.coffee +++ b/src/index.coffee @@ -49,6 +49,7 @@ $ -> if data.status == 'ok' that.fullReset() showAlert 'success', "successfully logged in '#{data.login}'" + location.reload() else switch data.text