JSON schema working
This commit is contained in:
parent
6306a4457d
commit
d9eb9796b1
2 changed files with 48 additions and 17 deletions
|
@ -12,6 +12,37 @@ fftcgdb = new FFTCGDB path.resolve(__dirname, 'fftcg.db'), true
|
||||||
# open session storage (volatile data)
|
# open session storage (volatile data)
|
||||||
session = new FFTCGSESSION
|
session = new FFTCGSESSION
|
||||||
|
|
||||||
|
JSONSCHEMA =
|
||||||
|
user: ->
|
||||||
|
body:
|
||||||
|
login: type: 'string'
|
||||||
|
password: type: 'string'
|
||||||
|
|
||||||
|
response:
|
||||||
|
200:
|
||||||
|
type: 'object'
|
||||||
|
required: ['success']
|
||||||
|
properties:
|
||||||
|
success: type: 'boolean'
|
||||||
|
err: type: 'string'
|
||||||
|
user:
|
||||||
|
type: 'object'
|
||||||
|
required: ['user', 'login']
|
||||||
|
properties:
|
||||||
|
user: type: 'number'
|
||||||
|
login: type: 'string'
|
||||||
|
# user is required iff success
|
||||||
|
# err is required otherwise
|
||||||
|
if:
|
||||||
|
properties:
|
||||||
|
success:
|
||||||
|
const: true
|
||||||
|
then:
|
||||||
|
required: ['user']
|
||||||
|
else:
|
||||||
|
required: ['err']
|
||||||
|
|
||||||
|
|
||||||
module.exports = [
|
module.exports = [
|
||||||
# test
|
# test
|
||||||
url: '/test'
|
url: '/test'
|
||||||
|
@ -29,31 +60,34 @@ module.exports = [
|
||||||
# register user
|
# register user
|
||||||
url: '/user/register'
|
url: '/user/register'
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
|
schema: JSONSCHEMA.user()
|
||||||
|
|
||||||
handler: (request, reply) ->
|
handler: (request, reply) ->
|
||||||
fftcgdb.register request.body.login, request.body.password
|
fftcgdb.register request.body.login, request.body.password
|
||||||
.then (user) ->
|
.then (user) ->
|
||||||
# registration successful
|
logger.info "OK registration '#{request.body.login}'"
|
||||||
reply.send
|
reply.send
|
||||||
status: 'ok'
|
success: true
|
||||||
user: user.user
|
user: user
|
||||||
login: user.login
|
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
# registration failed
|
logger.debug "FAIL registration '#{request.body.login}'"
|
||||||
reply.send
|
reply.send
|
||||||
status: 'fail'
|
success: false
|
||||||
text: err
|
err: err
|
||||||
,
|
,
|
||||||
# log in user
|
# log in user
|
||||||
url: '/user/login'
|
url: '/user/login'
|
||||||
method: 'POST'
|
method: 'POST'
|
||||||
|
schema: JSONSCHEMA.user()
|
||||||
|
|
||||||
handler: (request, reply) ->
|
handler: (request, reply) ->
|
||||||
session_id = request.cookies.session ? ''
|
session_id = request.cookies.session ? ''
|
||||||
|
|
||||||
new Promise (resolve, reject) ->
|
new Promise (resolve) ->
|
||||||
session.check session_id
|
session.check session_id
|
||||||
.then (user) ->
|
.then (user) ->
|
||||||
logger.debug "user '#{user.login}' resumed session '#{session_id}'"
|
logger.debug "OK '#{user.login}' resumed session '#{session_id}'"
|
||||||
resolve user
|
resolve user
|
||||||
|
|
||||||
.catch ->
|
.catch ->
|
||||||
|
@ -62,21 +96,19 @@ module.exports = [
|
||||||
# login successful: start new session
|
# login successful: start new session
|
||||||
session.start user
|
session.start user
|
||||||
.then (session_id) ->
|
.then (session_id) ->
|
||||||
logger.debug "user '#{user.login}' logged into session '#{session_id}'"
|
logger.info "OK '#{user.login}' created session '#{session_id}'"
|
||||||
reply.setCookie 'session', session_id
|
reply.setCookie 'session', session_id
|
||||||
resolve user
|
resolve user
|
||||||
|
|
||||||
.catch (err) ->
|
.catch (err) ->
|
||||||
logger.info "failed login for '#{request.body.login}'"
|
logger.info "FAIL login for '#{request.body.login}'"
|
||||||
# login failed
|
|
||||||
reply.send
|
reply.send
|
||||||
status: 'fail'
|
success: false
|
||||||
msg: err
|
msg: err
|
||||||
|
|
||||||
.then (user) ->
|
.then (user) ->
|
||||||
# user is logged in
|
|
||||||
reply.send
|
reply.send
|
||||||
status: 'ok'
|
success: true
|
||||||
user: user
|
user: user
|
||||||
,
|
,
|
||||||
]
|
]
|
||||||
|
|
|
@ -11,8 +11,7 @@ routes = (require './routes')
|
||||||
# fastify framework
|
# fastify framework
|
||||||
fastify.register (require 'fastify-cookie')
|
fastify.register (require 'fastify-cookie')
|
||||||
fastify.register (require 'fastify-ws'), library: 'uws'
|
fastify.register (require 'fastify-ws'), library: 'uws'
|
||||||
fastify.register (require 'fastify-static'), root: path.join __dirname, 'tmpfront'
|
fastify.register (require 'fastify-static'), root: (path.join __dirname, 'tmpfront')
|
||||||
|
|
||||||
|
|
||||||
fastify.ready()
|
fastify.ready()
|
||||||
.then ->
|
.then ->
|
||||||
|
|
Reference in a new issue