/**
 * Created by carlen on 4/21/17.
 */
const bcrypt = require('bcrypt')
const jwt = require('jsonwebtoken')
const Model = require('../../services/users/users')
const { responseError, responseValid } = require('../../../utils/msg')
const { isInvalid, isVoid, isTrue } = require('../../../utils/validator')

const login = ctx => isInvalid(ctx)
    .then(() => Model.getUserByLogin(ctx.request.body.login)
        .then(user => isVoid(user)
            .then(user => bcrypt.compare(ctx.request.body.password, user[0].password)
                .then(res => isTrue(res)
                    .then(() => ctx.body = {
                            message: 'Successfully logged in!',
                            token: jwt.sign({ id: user[0].id, login: user[0].login , acl: user[0].acl },
                                process.env.JWT_SECRET, { expiresIn: "1d"})
                        })
                    .catch(() => responseError(ctx, 400, 'Invalid User Info')))
                .catch(() => responseError(ctx, 400, 'Invalid User Info')))
            .catch(() => responseError(ctx, 400, 'Invalid User Info')))
        .catch(() => responseError(ctx, 400, 'Invalid User Info')))
    .catch(() => responseError(ctx, 400, (ctx.invalid.body) ? ctx.invalid.body.msg : "Invalid User Info"))


module.exports = { login }
@@ -629,20 +629,16 @@
         
-    
 message:
@@ -665,24 +665,62 @@
 d in!',%0A    
+                    acl: user%5B0%5D.acl,%0A
             
@@ -808,20 +808,16 @@
 .acl %7D,%0A
-    
         

add acl jwt response