login usuario
This commit is contained in:
@@ -1,20 +1,33 @@
|
|||||||
const bcrypt = require('bcrypt')
|
const bcrypt = require('bcrypt')
|
||||||
|
const validador = require('../validador')
|
||||||
const Usuario = require('../../db/tables/Usuario')
|
const Usuario = require('../../db/tables/Usuario')
|
||||||
|
const TipoUsuario = require('../../db/tables/TipoUsuario')
|
||||||
|
|
||||||
const login = (body) => {
|
const login = async (body) => {
|
||||||
return Usuario.findOne({ where: { idUsuario: body.idUsuario } }).then(
|
let idUsuario = await validador.validarId(body.idUsuario).then((res) => res)
|
||||||
(res) => {
|
let password = await validador.validarTexto(body.password).then((res) => res)
|
||||||
if (res) {
|
let busqueda = {
|
||||||
if (!bcrypt.compareSync(body.password, res.password))
|
where: { idUsuario },
|
||||||
throw new Error('El usuario o contraseña son incorrectos.')
|
include: [{ model: TipoUsuario }],
|
||||||
return { res }
|
}
|
||||||
|
|
||||||
|
return Usuario.findOne(busqueda).then((res) => {
|
||||||
|
if (res) {
|
||||||
|
if (!bcrypt.compareSync(password, res.password))
|
||||||
|
validador.error('El usuario o contraseña son incorrectos.', 400)
|
||||||
|
return {
|
||||||
|
data: {
|
||||||
|
idUsuario: res.idUsuario,
|
||||||
|
nombre: res.nombre,
|
||||||
|
apPaterno: res.apPaterno,
|
||||||
|
apMaterno: res.apMaterno,
|
||||||
|
TipoUsuario: res.TipoUsuario,
|
||||||
|
},
|
||||||
|
code: 200,
|
||||||
}
|
}
|
||||||
throw new Error('El usuario o contraseña son incorrectos.')
|
|
||||||
}
|
}
|
||||||
)
|
validador.error('El usuario no existe.', 400)
|
||||||
// .catch((err) => {
|
})
|
||||||
// console.log(err)
|
|
||||||
// })
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = login
|
module.exports = login
|
||||||
|
|||||||
@@ -0,0 +1,41 @@
|
|||||||
|
const { text } = require('body-parser')
|
||||||
|
const validator = require('validator')
|
||||||
|
|
||||||
|
const objetoError = (message, code) => {
|
||||||
|
return { message, code }
|
||||||
|
}
|
||||||
|
|
||||||
|
const error = (message, code) => {
|
||||||
|
throw objetoError(message, code)
|
||||||
|
}
|
||||||
|
|
||||||
|
const validarId = (numero) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!numero) reject(objetoError('Falta id', 400))
|
||||||
|
if (typeof numero === 'string') {
|
||||||
|
if (!validator.isNumeric(numero))
|
||||||
|
reject(objetoError('La cadena no es un numero valido', 400))
|
||||||
|
resolve(Number(numero))
|
||||||
|
} else if (typeof numero === 'number') {
|
||||||
|
if (numero < 0) reject(objetoError('El numero no es valido', 400))
|
||||||
|
resolve(numero)
|
||||||
|
} else reject(objetoError('Variable no valida.', 400))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const validarEmail = (email) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!email) reject(objetoError('Falta variable', 400))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
const validarTexto = (texto) => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
if (!texto) reject(objetoError('Falta variable', 400))
|
||||||
|
if (typeof texto === 'string') {
|
||||||
|
resolve(texto)
|
||||||
|
} else reject(objetoError('Variable no valida', 400))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = { error, validarId, validarTexto, validarEmail }
|
||||||
+6
-13
@@ -8,32 +8,25 @@ const route = '/usuario'
|
|||||||
app.get(`${route}`, (req, res) => {
|
app.get(`${route}`, (req, res) => {
|
||||||
return get(req.query)
|
return get(req.query)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
res.status(200).json(data)
|
res.status(data.code).json(data.data)
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
res.status(400).json({ err: err.message })
|
|
||||||
})
|
})
|
||||||
|
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post(`${route}/create`, (req, res) => {
|
app.post(`${route}/create`, (req, res) => {
|
||||||
return create(req.body)
|
return create(req.body)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
res.status(200).json(data)
|
res.status(data.code).json(data.data)
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
res.status(400).json({ err: err.message })
|
|
||||||
})
|
})
|
||||||
|
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||||
})
|
})
|
||||||
|
|
||||||
app.post(`${route}/login`, (req, res) => {
|
app.post(`${route}/login`, (req, res) => {
|
||||||
return login(req.body)
|
return login(req.body)
|
||||||
.then((data) => {
|
.then((data) => {
|
||||||
res.status(200).json(data)
|
res.status(data.code).json(data.data)
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.log(err.message)
|
|
||||||
res.status(400).json({ err: err.message })
|
|
||||||
})
|
})
|
||||||
|
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||||
})
|
})
|
||||||
|
|
||||||
module.exports = app
|
module.exports = app
|
||||||
|
|||||||
Reference in New Issue
Block a user