2018-12-16 02:02:57 +00:00
|
|
|
# node libraries
|
2018-12-16 21:51:08 +00:00
|
|
|
express = (require 'express')
|
2018-12-16 02:02:57 +00:00
|
|
|
path = (require 'path')
|
|
|
|
|
|
|
|
# my libraries
|
2018-12-27 11:19:19 +00:00
|
|
|
FFTCGDB = (require './db')
|
2019-02-07 16:03:20 +00:00
|
|
|
logger = (require 'logging').default 'router'
|
2018-12-16 02:02:57 +00:00
|
|
|
|
|
|
|
# open fftcg db
|
2019-02-07 16:03:20 +00:00
|
|
|
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
|
2018-12-16 02:02:57 +00:00
|
|
|
|
2018-12-16 21:51:08 +00:00
|
|
|
# create router
|
|
|
|
FFTCGROUTER = express.Router()
|
2018-12-26 23:16:59 +00:00
|
|
|
|
|
|
|
# request logging
|
|
|
|
FFTCGROUTER.use (req, res, next) ->
|
|
|
|
if req.session.user
|
2019-02-07 16:03:20 +00:00
|
|
|
logger.debug "user '#{req.session.user.login}' requested '#{req.url}'"
|
2018-12-26 23:16:59 +00:00
|
|
|
else
|
2019-02-07 16:03:20 +00:00
|
|
|
logger.debug "requested '#{req.url}'"
|
2018-12-26 23:16:59 +00:00
|
|
|
|
|
|
|
next()
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
# register user
|
|
|
|
FFTCGROUTER.post '/register', (req, res) ->
|
|
|
|
fftcgdb.register req.body.login, req.body.password
|
2018-12-16 21:51:08 +00:00
|
|
|
.then (user) ->
|
|
|
|
# registration successful, return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'ok'
|
2018-12-27 02:03:22 +00:00
|
|
|
user: user.user
|
2018-12-16 21:51:08 +00:00
|
|
|
login: user.login
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
.catch (err) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
# registration failed, return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'fail'
|
|
|
|
text: err
|
|
|
|
|
|
|
|
# log in user
|
|
|
|
FFTCGROUTER.post '/login', (req, res) ->
|
|
|
|
fftcgdb.login req.body.login, req.body.password
|
2018-12-16 21:51:08 +00:00
|
|
|
.then (user) ->
|
|
|
|
# login successful, save stuff in session
|
|
|
|
req.session.user = user
|
2018-12-16 02:02:57 +00:00
|
|
|
req.session.save()
|
2018-12-16 21:51:08 +00:00
|
|
|
|
|
|
|
# return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'ok'
|
2018-12-27 02:03:22 +00:00
|
|
|
user: user.user
|
2018-12-16 21:51:08 +00:00
|
|
|
login: user.login
|
|
|
|
|
2018-12-16 02:02:57 +00:00
|
|
|
.catch (err) ->
|
2018-12-16 21:51:08 +00:00
|
|
|
# login failed, return JSON status
|
2018-12-16 02:02:57 +00:00
|
|
|
res.json
|
|
|
|
status: 'fail'
|
|
|
|
text: err
|
|
|
|
|
|
|
|
module.exports = FFTCGROUTER
|