-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
338 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
PORT=8080 | ||
MONGODB_CNN= | ||
SECRETORPRIVATEKEY= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
const { response } = require('express'); | ||
const bcryptjs = require('bcryptjs') | ||
|
||
const Usuario = require('../models/usuario'); | ||
|
||
const { generarJWT } = require('../helpers/generar-jwt'); | ||
|
||
|
||
const login = async(req, res = response) => { | ||
|
||
const { correo, password } = req.body; | ||
|
||
try { | ||
|
||
// Verificar si el email existe | ||
const usuario = await Usuario.findOne({ correo }); | ||
if ( !usuario ) { | ||
return res.status(400).json({ | ||
msg: 'Usuario / Password no son correctos - correo' | ||
}); | ||
} | ||
|
||
// SI el usuario está activo | ||
if ( !usuario.estado ) { | ||
return res.status(400).json({ | ||
msg: 'Usuario / Password no son correctos - estado: false' | ||
}); | ||
} | ||
|
||
// Verificar la contraseña | ||
const validPassword = bcryptjs.compareSync( password, usuario.password ); | ||
if ( !validPassword ) { | ||
return res.status(400).json({ | ||
msg: 'Usuario / Password no son correctos - password' | ||
}); | ||
} | ||
|
||
// Generar el JWT | ||
const token = await generarJWT( usuario.id ); | ||
|
||
res.json({ | ||
usuario, | ||
token | ||
}) | ||
|
||
} catch (error) { | ||
console.log(error) | ||
res.status(500).json({ | ||
msg: 'Hable con el administrador' | ||
}); | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
module.exports = { | ||
login | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
|
||
|
||
const generarJWT = ( uid = '' ) => { | ||
|
||
return new Promise( (resolve, reject) => { | ||
|
||
const payload = { uid }; | ||
|
||
jwt.sign( payload, process.env.SECRETORPRIVATEKEY, { | ||
expiresIn: '4h' | ||
}, ( err, token ) => { | ||
|
||
if ( err ) { | ||
console.log(err); | ||
reject( 'No se pudo generar el token' ) | ||
} else { | ||
resolve( token ); | ||
} | ||
}) | ||
|
||
}) | ||
} | ||
|
||
|
||
|
||
|
||
module.exports = { | ||
generarJWT | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
|
||
|
||
const validaCampos = require('../middlewares/validar-campos'); | ||
const validarJWT = require('../middlewares/validar-jwt'); | ||
const validaRoles = require('../middlewares/validar-roles'); | ||
|
||
module.exports = { | ||
...validaCampos, | ||
...validarJWT, | ||
...validaRoles, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
const { response, request } = require('express'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const Usuario = require('../models/usuario'); | ||
|
||
|
||
const validarJWT = async( req = request, res = response, next ) => { | ||
|
||
const token = req.header('x-token'); | ||
|
||
if ( !token ) { | ||
return res.status(401).json({ | ||
msg: 'No hay token en la petición' | ||
}); | ||
} | ||
|
||
try { | ||
|
||
const { uid } = jwt.verify( token, process.env.SECRETORPRIVATEKEY ); | ||
|
||
// leer el usuario que corresponde al uid | ||
const usuario = await Usuario.findById( uid ); | ||
|
||
if( !usuario ) { | ||
return res.status(401).json({ | ||
msg: 'Token no válido - usuario no existe DB' | ||
}) | ||
} | ||
|
||
// Verificar si el uid tiene estado true | ||
if ( !usuario.estado ) { | ||
return res.status(401).json({ | ||
msg: 'Token no válido - usuario con estado: false' | ||
}) | ||
} | ||
|
||
|
||
req.usuario = usuario; | ||
next(); | ||
|
||
} catch (error) { | ||
|
||
console.log(error); | ||
res.status(401).json({ | ||
msg: 'Token no válido' | ||
}) | ||
} | ||
|
||
} | ||
|
||
|
||
|
||
|
||
module.exports = { | ||
validarJWT | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
const { response } = require('express') | ||
|
||
|
||
const esAdminRole = ( req, res = response, next ) => { | ||
|
||
if ( !req.usuario ) { | ||
return res.status(500).json({ | ||
msg: 'Se quiere verificar el role sin validar el token primero' | ||
}); | ||
} | ||
|
||
const { rol, nombre } = req.usuario; | ||
|
||
if ( rol !== 'ADMIN_ROLE' ) { | ||
return res.status(401).json({ | ||
msg: `${ nombre } no es administrador - No puede hacer esto` | ||
}); | ||
} | ||
|
||
next(); | ||
} | ||
|
||
|
||
const tieneRole = ( ...roles ) => { | ||
return (req, res = response, next) => { | ||
|
||
if ( !req.usuario ) { | ||
return res.status(500).json({ | ||
msg: 'Se quiere verificar el role sin validar el token primero' | ||
}); | ||
} | ||
|
||
if ( !roles.includes( req.usuario.rol ) ) { | ||
return res.status(401).json({ | ||
msg: `El servicio requiere uno de estos roles ${ roles }` | ||
}); | ||
} | ||
|
||
|
||
next(); | ||
} | ||
} | ||
|
||
|
||
|
||
module.exports = { | ||
esAdminRole, | ||
tieneRole | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.