Created
August 19, 2019 21:15
-
-
Save asfo/fdbfcc2552a5ae5969cc8f0fc2d6174c to your computer and use it in GitHub Desktop.
index.js para el tutorial de Medium acerca de la seguridad de APIs con JWT
This file contains hidden or 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
const express = require('express'), | |
bodyParser = require('body-parser'), | |
jwt = require('jsonwebtoken'), | |
app = express(); | |
const config = { | |
llave : "miclaveultrasecreta123*" | |
}; | |
// 1 | |
app.set('llave', config.llave); | |
// 2 | |
app.use(bodyParser.urlencoded({ extended: true })); | |
// 3 | |
app.use(bodyParser.json()); | |
app.listen(3000,()=>{ | |
console.log('Servidor iniciado en el puerto 3000') | |
}); | |
// 4 | |
app.get('/', function(req, res) { | |
res.json({ message: 'recurso de entrada' }); | |
}); | |
// 5 | |
app.post('/autenticar', (req, res) => { | |
if(req.body.usuario === "asfo" && req.body.contrasena === "holamundo") { | |
const payload = { | |
check: true | |
}; | |
const token = jwt.sign(payload, app.get('llave'), { | |
expiresIn: 1440 | |
}); | |
res.json({ | |
mensaje: 'Autenticación correcta', | |
token: token | |
}); | |
} else { | |
res.json({ mensaje: "Usuario o contraseña incorrectos"}) | |
} | |
}) | |
// 6 | |
const rutasProtegidas = express.Router(); | |
rutasProtegidas.use((req, res, next) => { | |
const token = req.headers['access-token']; | |
if (token) { | |
jwt.verify(token, app.get('llave'), (err, decoded) => { | |
if (err) { | |
return res.json({ mensaje: 'Token inválida' }); | |
} else { | |
req.decoded = decoded; | |
next(); | |
} | |
}); | |
} else { | |
res.send({ | |
mensaje: 'Token no proveída.' | |
}); | |
} | |
}); | |
app.get('/datos', rutasProtegidas, (req, res) => { | |
const datos = [ | |
{ id: 1, nombre: "Asfo" }, | |
{ id: 2, nombre: "Denisse" }, | |
{ id: 3, nombre: "Carlos" } | |
]; | |
res.json(datos); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment