From e78570f298c5c5deeac2d2e9379386c79b2c9be3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=B6rn-Michael=20Miehe?= Date: Sat, 16 Feb 2019 19:03:21 +0100 Subject: [PATCH] routes for basic user mgmt --- backend/routes.coffee | 62 ++++++++++++++++++++++-------------------- backend/server.coffee | 10 +++++-- backend/session.coffee | 2 +- backend/yarn.lock | 16 +++++------ 4 files changed, 49 insertions(+), 41 deletions(-) diff --git a/backend/routes.coffee b/backend/routes.coffee index 0590fc8..2f6edcc 100644 --- a/backend/routes.coffee +++ b/backend/routes.coffee @@ -1,5 +1,5 @@ # node libraries -#(require 'debug').enable 'routes' +(require 'debug').enable 'routes' logger = (require 'logging').default 'routes' path = (require 'path') @@ -63,18 +63,19 @@ module.exports = [ schema: JSONSCHEMA.user() handler: (request, reply) -> - fftcgdb.register request.body.login, request.body.password - .then (user) -> - logger.info "OK registration '#{request.body.login}'" - reply.send - success: true - user: user + fftcgdb.register(request.body.login, request.body.password) + .then (user) -> + logger.info "OK registration '#{request.body.login}'" + reply.send + success: true + user: user + return - .catch (err) -> - logger.debug "FAIL registration '#{request.body.login}'" - reply.send - success: false - err: err + .catch (err) -> + logger.debug "FAIL registration '#{request.body.login}'" + reply.send + success: false + err: err , # log in user url: '/user/login' @@ -86,27 +87,30 @@ module.exports = [ new Promise (resolve) -> session.check session_id + .then (user) -> + # active session found + logger.debug "OK '#{user.login}' resumed session '#{session_id}'" + resolve user + + .catch -> + fftcgdb.login request.body.login, request.body.password .then (user) -> - logger.debug "OK '#{user.login}' resumed session '#{session_id}'" - resolve user + # login successful: start new session + session.start user + .then (session_id) -> + logger.info "OK '#{user.login}' created session '#{session_id}'" + reply.setCookie 'session', session_id + resolve user - .catch -> - fftcgdb.login request.body.login, request.body.password - .then (user) -> - # login successful: start new session - session.start user - .then (session_id) -> - logger.info "OK '#{user.login}' created session '#{session_id}'" - reply.setCookie 'session', session_id - resolve user - - .catch (err) -> - logger.info "FAIL login for '#{request.body.login}'" - reply.send - success: false - msg: err + .catch (err) -> + # login failed + logger.info "FAIL login for '#{request.body.login}'" + reply.send + success: false + err: err .then (user) -> + # tell about the user who logged in reply.send success: true user: user diff --git a/backend/server.coffee b/backend/server.coffee index a60ace8..0937a61 100644 --- a/backend/server.coffee +++ b/backend/server.coffee @@ -1,6 +1,8 @@ # node libraries +# (require 'debug').enable 'FFTCG' logger = (require 'logging').default 'FFTCG' -fastify = (require 'fastify') logger: logger +fastify = (require 'fastify') + logger: level: 'warn' path = (require 'path') # my libraries @@ -21,9 +23,11 @@ fastify.ready() logger.error err process.exit 1 +fastify.addHook 'onRequest', (req, res, next) -> + logger.debug 'requested', req.url + next() -for route in routes - fastify.route route +fastify.route route for route in routes fastify.listen 3001, '0.0.0.0' .catch (err) -> diff --git a/backend/session.coffee b/backend/session.coffee index 98f2295..2c4b8a4 100644 --- a/backend/session.coffee +++ b/backend/session.coffee @@ -5,7 +5,7 @@ logger = (require 'logging').default 'session' # expiry times in seconds EXPIRY = - # games expire 1 week after last action + # games expire 1 week after creation game: 1 * 60 * 60 * 24 * 7 # logins expire 1 month after last action login: 1 * 60 * 60 * 24 * 7 * 4 diff --git a/backend/yarn.lock b/backend/yarn.lock index 4ad05af..9e8d6fb 100644 --- a/backend/yarn.lock +++ b/backend/yarn.lock @@ -345,10 +345,10 @@ chalk@^2.0.1, chalk@^2.4.1: escape-string-regexp "^1.0.5" supports-color "^5.3.0" -chokidar@^2.0.4: - version "2.1.0" - resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.0.tgz#5fcb70d0b28ebe0867eb0f09d5f6a08f29a1efa0" - integrity sha512-5t6G2SH8eO6lCvYOoUpaRnF5Qfd//gd7qJAkwRUw9qlGVkiQ13uwQngqbWWaurOsaAm9+kUGbITADxt6H0XFNQ== +chokidar@^2.1.0: + version "2.1.1" + resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-2.1.1.tgz#adc39ad55a2adf26548bd2afa048f611091f9184" + integrity sha512-gfw3p2oQV2wEt+8VuMlNsPjCxDxvvgnm/kz+uATu805mWVF8IJN7uz9DN7iBz+RMJISmiVbCOBFs9qBGMjtPfQ== dependencies: anymatch "^2.0.0" async-each "^1.0.1" @@ -1629,11 +1629,11 @@ node-pre-gyp@^0.11.0: tar "^4" nodemon@^1.18.9: - version "1.18.9" - resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-1.18.9.tgz#90b467efd3b3c81b9453380aeb2a2cba535d0ead" - integrity sha512-oj/eEVTEI47pzYAjGkpcNw0xYwTl4XSTUQv2NPQI6PpN3b75PhpuYk3Vb3U80xHCyM2Jm+1j68ULHXl4OR3Afw== + version "1.18.10" + resolved "https://registry.yarnpkg.com/nodemon/-/nodemon-1.18.10.tgz#3ba63f64eb4c283cf3e4f75f30817e9d4f393afe" + integrity sha512-we51yBb1TfEvZamFchRgcfLbVYgg0xlGbyXmOtbBzDwxwgewYS/YbZ5tnlnsH51+AoSTTsT3A2E/FloUbtH8cQ== dependencies: - chokidar "^2.0.4" + chokidar "^2.1.0" debug "^3.1.0" ignore-by-default "^1.0.1" minimatch "^3.0.4"