Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Rota para autenticar usuarios
- @method POST
- @path /api/auth
- @content-type json
- @params ['username', 'password', 'user_agent', 'ip']
- @exemplo curl -H 'Content-Type: application/json' -X POST -d '{"username": "nine", "password": "pass2342"}' http://localhost:8099/api/auth
- */
- app.post('/api/auth', (req, res) => {
- var schema = Joi.object().keys({
- username: Joi.string().min(4).max(16).required(),
- password: Joi.string().min(4).max(16).required(),
- user_agent: Joi.string().required(),
- ip: Joi.string().required()
- })
- Joi.validate(req.body, schema, (err, value) => {
- if(err) {
- res.json(({err: true, message: err.details[0].message, response: null}))
- return
- }
- User.auth(req.body.username, req.body.password).then((auth_res) => {
- if(auth_res.err == false) {
- Session.start(auth_res.response.id, req.body.user_agent, req.body.ip, 10).then((session_res) => {
- res.json(session_res)
- })
- }
- else {
- res.json(auth_res)
- }
- })
- })
- })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement