44 lines
1.1 KiB
CoffeeScript
44 lines
1.1 KiB
CoffeeScript
# 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
|