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/routes.coffee

76 lines
1.7 KiB
CoffeeScript
Raw Normal View History

# node libraries
path = (require 'path')
# my libraries
FFTCGDB = (require './db')
FFTCGSESSION = (require './session')
logger = (require 'logging').default 'routes'
# open fftcg.db (persistent data)
fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db'), true
# open session storage (volatile data)
session = new FFTCGSESSION
module.exports = [
# test
url: '/test'
method: 'POST'
handler: (request, reply) ->
logger.info 'Cookies', request.cookies
logger.info 'Body', request.body
logger.info 'Query', request.query
logger.info 'Params', request.params
reply.setCookie 'foo', 'foo'
reply.send
hello: 'world'
,
# register user
url: '/user/register'
method: 'POST'
handler: (request, reply) ->
fftcgdb.register request.body.login, request.body.password
.then (user) ->
# registration successful
reply.send
status: 'ok'
user: user.user
login: user.login
.catch (err) ->
# registration failed
reply.send
status: 'fail'
text: err
,
# log in user
url: '/user/login'
method: 'POST'
handler: (request, reply) ->
session_id = request.cookies.session
logger.info session_id
session.action session_id
.then (user) ->
logger.info user
.catch (err) ->
logger.error err
fftcgdb.login request.body.login, request.body.password
.then (user) ->
# login successful
reply.setCookie 'user', JSON.stringify user
# return JSON status
reply.send
status: 'ok'
user: user.user
login: user.login
.catch (err) ->
# login failed
reply.send
status: 'fail'
text: err
,
]