login usuario
This commit is contained in:
@@ -12,7 +12,8 @@ export class UsuarioController {
|
||||
correoAlternativo,
|
||||
numeroTelefonico,
|
||||
identificacion,
|
||||
institucion
|
||||
institucion,
|
||||
secreto
|
||||
} = req.body
|
||||
|
||||
const usuario = new Usuario()
|
||||
@@ -24,6 +25,9 @@ export class UsuarioController {
|
||||
usuario.numeroTelefonico = numeroTelefonico
|
||||
usuario.identificacion = identificacion
|
||||
usuario.institucion = institucion
|
||||
usuario.secreto = secreto
|
||||
|
||||
usuario.hashPassword()
|
||||
|
||||
const errors = await validate(usuario)
|
||||
if (errors.length > 0) {
|
||||
@@ -60,4 +64,29 @@ export class UsuarioController {
|
||||
res.status(404).send('Usuario no encontrado')
|
||||
}
|
||||
}
|
||||
|
||||
static async login (req: Request, res: Response) {
|
||||
const repository = getRepository(Usuario)
|
||||
const { id, secreto } = req.body
|
||||
try {
|
||||
const usuario = await repository.findOne({
|
||||
where: {
|
||||
id
|
||||
}
|
||||
})
|
||||
if (!usuario) {
|
||||
throw new Error('*Usuario o contraseña invalidos')
|
||||
}
|
||||
if (!usuario.checkIfUnencryptedPasswordIsValid(secreto)) {
|
||||
throw new Error('Usuario o *contraseña invalidos')
|
||||
}
|
||||
res.status(200).json({
|
||||
nombre: usuario.nombre,
|
||||
apellidoPaterno: usuario.apellidoPaterno,
|
||||
apellidoMaterno: usuario.apellidoMaterno
|
||||
})
|
||||
} catch (err) {
|
||||
res.status(400).send(err.message)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
import * as bcrypt from 'bcrypt'
|
||||
|
||||
import {
|
||||
Entity,
|
||||
PrimaryGeneratedColumn,
|
||||
@@ -53,6 +55,10 @@ export class Usuario {
|
||||
@IsString()
|
||||
institucion: string
|
||||
|
||||
@Column()
|
||||
@IsString()
|
||||
secreto: string
|
||||
|
||||
@Column({ type: 'date', default: '1996-11-07' })
|
||||
@IsDate()
|
||||
@IsOptional()
|
||||
@@ -93,4 +99,12 @@ export class Usuario {
|
||||
(log) => log.usuario
|
||||
)
|
||||
logs: Log[]
|
||||
|
||||
hashPassword () {
|
||||
this.secreto = bcrypt.hashSync(this.secreto, 10)
|
||||
}
|
||||
|
||||
checkIfUnencryptedPasswordIsValid (unencryptedPassword: string) {
|
||||
return bcrypt.compareSync(unencryptedPassword, this.secreto)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,6 +5,8 @@ const router = Router()
|
||||
|
||||
router.post('/', UsuarioController.create)
|
||||
|
||||
router.post('/login', UsuarioController.login)
|
||||
|
||||
router.get('/', UsuarioController.getAll)
|
||||
|
||||
router.get('/:id([0-9]+)', UsuarioController.getOne)
|
||||
|
||||
Reference in New Issue
Block a user