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/inc/fftcgrouter.coffee

45 lines
1.1 KiB
CoffeeScript
Raw Normal View History

2018-12-16 02:02:57 +00:00
# node libraries
FFTCGROUTER = (require 'express').Router()
path = (require 'path')
# my libraries
FFTCGDB = (require './fftcgdb')
# open fftcg db
fftcgdb = new FFTCGDB path.resolve(__dirname, '../fftcg.db')
# register user
FFTCGROUTER.post '/register', (req, res) ->
fftcgdb.register req.body.login, req.body.password
.then (userid) ->
console.log "registered '#{req.body.login}'"
res.json
status: 'ok'
uid: userid
text: req.body.login
.catch (err) ->
console.log "failed to register '#{req.body.login}'"
res.json
status: 'fail'
text: err
# log in user
FFTCGROUTER.post '/login', (req, res) ->
fftcgdb.login req.body.login, req.body.password
.then (userid) ->
req.session.userID = userid
req.session.userLogin = req.body.login
req.session.save()
console.log "logged in '#{req.body.login}'"
res.json
status: 'ok'
uid: userid
text: req.body.login
.catch (err) ->
console.log "failed to login '#{req.body.login}'"
res.json
status: 'fail'
text: err
module.exports = FFTCGROUTER