This repository has been archived on 2024-04-29. You can view files and clone it, but cannot push or open issues or pull requests.
node-fftcg/backend/router.coffee

61 lines
1.3 KiB
CoffeeScript
Raw Normal View History

2018-12-16 02:02:57 +00:00
# node libraries
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
# create router
FFTCGROUTER = express.Router()
# 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}'"
else
2019-02-07 16:03:20 +00:00
logger.debug "requested '#{req.url}'"
next()
2018-12-16 02:02:57 +00:00
# register user
FFTCGROUTER.post '/register', (req, res) ->
fftcgdb.register req.body.login, req.body.password
.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
login: user.login
2018-12-16 02:02:57 +00:00
.catch (err) ->
# 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
.then (user) ->
# login successful, save stuff in session
req.session.user = user
2018-12-16 02:02:57 +00:00
req.session.save()
# 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
login: user.login
2018-12-16 02:02:57 +00:00
.catch (err) ->
# login failed, return JSON status
2018-12-16 02:02:57 +00:00
res.json
status: 'fail'
text: err
module.exports = FFTCGROUTER