# node libraries
express = (require 'express')
path = (require 'path')
# my libraries
FFTCGDB = (require './fftcgdb')
# open fftcg db
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
# create router
FFTCGROUTER = express.Router()
# FFTCGROUTER.use express.static path.resolve(__dirname, '../public_html')
# 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'
uid: user.id
login: user.login
.catch (err) ->
# registration failed, return JSON status
status: 'fail'
text: err
# log in user
FFTCGROUTER.post '/login', (req, res) ->
fftcgdb.login req.body.login, req.body.password
# login successful, save stuff in session
req.session.user = user
req.session.save()
# return JSON status
uid: user.uid
# login failed, return JSON status
module.exports = FFTCGROUTER