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

52 lines
1.2 KiB
CoffeeScript
Raw Normal View History

2018-12-16 02:02:57 +00:00
# node libraries
express = (require 'express')
2018-12-16 02:02:57 +00:00
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')
2018-12-16 02:02:57 +00:00
# register user
FFTCGROUTER.post '/register', (req, res) ->
fftcgdb.register req.body.login, req.body.password
.then (user) ->
# registration successful, return JSON status
2018-12-16 02:02:57 +00:00
res.json
status: 'ok'
uid: user.id
login: user.login
2018-12-16 02:02:57 +00:00
.catch (err) ->
# registration failed, return JSON status
2018-12-16 02:02:57 +00:00
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
2018-12-16 02:02:57 +00:00
req.session.save()
# return JSON status
2018-12-16 02:02:57 +00:00
res.json
status: 'ok'
uid: user.uid
login: user.login
2018-12-16 02:02:57 +00:00
.catch (err) ->
# login failed, return JSON status
2018-12-16 02:02:57 +00:00
res.json
status: 'fail'
text: err
module.exports = FFTCGROUTER