Advertisement
nin993

Untitled

Jun 5th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.     /*
  2.         Rota para autenticar usuarios
  3.  
  4.         @method         POST
  5.         @path           /api/auth
  6.         @content-type   json
  7.         @params         ['username', 'password', 'user_agent', 'ip']
  8.  
  9.         @exemplo curl -H 'Content-Type: application/json' -X POST -d '{"username": "nine", "password": "pass2342"}' http://localhost:8099/api/auth
  10.     */
  11.     app.post('/api/auth', (req, res) => {
  12.         var schema = Joi.object().keys({
  13.             username: Joi.string().min(4).max(16).required(),
  14.             password: Joi.string().min(4).max(16).required(),
  15.             user_agent: Joi.string().required(),
  16.             ip: Joi.string().required()
  17.         })
  18.  
  19.         Joi.validate(req.body, schema, (err, value) => {
  20.             if(err) {
  21.                 res.json(({err: true, message: err.details[0].message, response: null}))
  22.                 return
  23.             }
  24.  
  25.             User.auth(req.body.username, req.body.password).then((auth_res) => {
  26.                 if(auth_res.err == false) {
  27.                     Session.start(auth_res.response.id, req.body.user_agent, req.body.ip, 10).then((session_res) => {
  28.                         res.json(session_res)
  29.                     })
  30.                 }
  31.                 else {
  32.                     res.json(auth_res)
  33.                 }
  34.                
  35.             })
  36.         })
  37.     })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement