Skip to content

Instantly share code, notes, and snippets.

@suissa
Forked from higordiego/authenticate_old.js
Last active October 18, 2017 00:54
Show Gist options
  • Save suissa/a17079973a24b1f497e4e88feedaaff3 to your computer and use it in GitHub Desktop.
Save suissa/a17079973a24b1f497e4e88feedaaff3 to your computer and use it in GitHub Desktop.
const crypto = require('../../helpers/crypto')
const Errors = require('../../errors/sistem/pt-br')
const Validate = require('../../helpers/validate')
const authenticate = (User, Validate, Business, Errors) => (req, res, next) => {
const exclude = ['password',
'created_at',
'updated_at',
'master',
'token',
'forgot',
'active'
]
const query = {
attributes: {
exclude
},
where: {
$and: [{
email: req.body.email
}, {
password: crypto.md5(req.body.password)
}]
}
}
Validate.searchQuery(User, query)
.then(Validate.isEmptyObject(res, Errors.notAuthorization))
.then(Business.authenticate(res))
.catch(err => console.log(err))
}
module.exports = app => {
const User = app.datasource.models.User
const Business = require('../business/authenticate')(app)
return {
authenticate: authenticate(User, Validate, Business, Errors)
}
}
module.exports = app => {
const User = app.datasource.models.User
const Business = require('../business/authenticate')(app)
const crypto = require('../../helpers/crypto')
const Errors = require('../../errors/sistem/pt-br')
return {
authenticate: (req, res) => {
const query = {
attributes: {
exclude: ['password', 'created_at', 'updated_at', 'master', 'token', 'forgot', 'active']
},
where: {
$and: [{
email: req.body.email
}, {
password: crypto.md5(req.body.password)
}]
}
}
User.findOne(query)
.then(user =>
user
? Business.authenticate(user, res)
: res.status(401).json([Errors.notAuthorization])
).catch(err => console.log(err))
}
}
}
module.exports = app => {
const User = app.datasource.models.User
const Business = require('../business/authenticate')(app)
const crypto = require('../../helpers/crypto')
const Errors = require('../../errors/sistem/pt-br')
const Validate = require('../../helpers/validate')
return {
authenticate: (req, res) => {
const query = {
attributes: {
exclude: ['password', 'created_at', 'updated_at', 'master', 'token', 'forgot', 'active']
},
where: {
$and: [{
email: req.body.email
}, {
password: crypto.md5(req.body.password)
}]
}
}
Validate.searchQuery(User, query)
.then(Validate.isEmptyObject(res, Errors.notAuthorization))
.then(Business.authenticate(res))
.catch(err => console.log(err))
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment