60 lines
1.3 KiB
CoffeeScript
60 lines
1.3 KiB
CoffeeScript
# node libraries
|
|
express = (require 'express')
|
|
path = (require 'path')
|
|
|
|
# my libraries
|
|
FFTCGDB = (require './db')
|
|
logger = (require 'logging').default 'router'
|
|
|
|
# open fftcg db
|
|
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db')
|
|
|
|
# create router
|
|
FFTCGROUTER = express.Router()
|
|
|
|
# request logging
|
|
FFTCGROUTER.use (req, res, next) ->
|
|
if req.session.user
|
|
logger.debug "user '#{req.session.user.login}' requested '#{req.url}'"
|
|
else
|
|
logger.debug "requested '#{req.url}'"
|
|
|
|
next()
|
|
|
|
# register user
|
|
FFTCGROUTER.post '/register', (req, res) ->
|
|
fftcgdb.register req.body.login, req.body.password
|
|
.then (user) ->
|
|
# registration successful, return JSON status
|
|
res.json
|
|
status: 'ok'
|
|
user: user.user
|
|
login: user.login
|
|
|
|
.catch (err) ->
|
|
# registration failed, return JSON status
|
|
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
|
|
req.session.save()
|
|
|
|
# return JSON status
|
|
res.json
|
|
status: 'ok'
|
|
user: user.user
|
|
login: user.login
|
|
|
|
.catch (err) ->
|
|
# login failed, return JSON status
|
|
res.json
|
|
status: 'fail'
|
|
text: err
|
|
|
|
module.exports = FFTCGROUTER
|