login usuario
This commit is contained in:
@@ -1,20 +1,33 @@
|
||||
const bcrypt = require('bcrypt')
|
||||
const validador = require('../validador')
|
||||
const Usuario = require('../../db/tables/Usuario')
|
||||
const TipoUsuario = require('../../db/tables/TipoUsuario')
|
||||
|
||||
const login = (body) => {
|
||||
return Usuario.findOne({ where: { idUsuario: body.idUsuario } }).then(
|
||||
(res) => {
|
||||
if (res) {
|
||||
if (!bcrypt.compareSync(body.password, res.password))
|
||||
throw new Error('El usuario o contraseña son incorrectos.')
|
||||
return { res }
|
||||
const login = async (body) => {
|
||||
let idUsuario = await validador.validarId(body.idUsuario).then((res) => res)
|
||||
let password = await validador.validarTexto(body.password).then((res) => res)
|
||||
let busqueda = {
|
||||
where: { idUsuario },
|
||||
include: [{ model: TipoUsuario }],
|
||||
}
|
||||
|
||||
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.')
|
||||
}
|
||||
)
|
||||
// .catch((err) => {
|
||||
// console.log(err)
|
||||
// })
|
||||
validador.error('El usuario no existe.', 400)
|
||||
})
|
||||
}
|
||||
|
||||
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) => {
|
||||
return get(req.query)
|
||||
.then((data) => {
|
||||
res.status(200).json(data)
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(400).json({ err: err.message })
|
||||
res.status(data.code).json(data.data)
|
||||
})
|
||||
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||
})
|
||||
|
||||
app.post(`${route}/create`, (req, res) => {
|
||||
return create(req.body)
|
||||
.then((data) => {
|
||||
res.status(200).json(data)
|
||||
})
|
||||
.catch((err) => {
|
||||
res.status(400).json({ err: err.message })
|
||||
res.status(data.code).json(data.data)
|
||||
})
|
||||
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||
})
|
||||
|
||||
app.post(`${route}/login`, (req, res) => {
|
||||
return login(req.body)
|
||||
.then((data) => {
|
||||
res.status(200).json(data)
|
||||
})
|
||||
.catch((err) => {
|
||||
console.log(err.message)
|
||||
res.status(400).json({ err: err.message })
|
||||
res.status(data.code).json(data.data)
|
||||
})
|
||||
.catch((err) => res.status(err.code).json({ err: err.message }))
|
||||
})
|
||||
|
||||
module.exports = app
|
||||
|
||||
Reference in New Issue
Block a user