-
Notifications
You must be signed in to change notification settings - Fork 0
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
Lucas Serrey
committed
Dec 11, 2019
1 parent
3fd5b3d
commit 6757f66
Showing
9 changed files
with
247 additions
and
12 deletions.
There are no files selected for viewing
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
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,54 @@ | ||
const jwt = require('jsonwebtoken'); | ||
|
||
// ========================= | ||
// Verifica Token | ||
// ========================= | ||
let verificaToken = (req, res, next) => { | ||
|
||
let token = req.get('token'); | ||
|
||
jwt.verify(token, process.env.SEED, (err, decoded) => { | ||
|
||
if (err) { | ||
return res.status(401).json({ | ||
ok: false, | ||
err: { | ||
message: 'Token no válido' | ||
} | ||
}); | ||
} | ||
|
||
req.usuario = decoded.usuario; | ||
next(); | ||
|
||
}); | ||
}; | ||
|
||
// ========================= | ||
// Verifica AdminRole | ||
// ========================= | ||
let verificaAdminRole = (req, res, next) => { | ||
|
||
usuario = req.usuario; | ||
|
||
if(usuario.role != 'ADMIN_ROLE'){ | ||
|
||
res.json({ | ||
ok: false, | ||
err: { | ||
message: 'Debe ser administrador' | ||
} | ||
}); | ||
} | ||
else{ | ||
|
||
next(); | ||
} | ||
|
||
}; | ||
|
||
|
||
module.exports = { | ||
verificaToken, | ||
verificaAdminRole | ||
} |
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,7 @@ | ||
const express = require('express'); | ||
const app = express(); | ||
|
||
app.use(require('./usuario')); | ||
app.use(require('./login')); | ||
|
||
module.exports = app; |
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 express = require('express'); | ||
const bcrypt = require('bcrypt'); | ||
const jwt = require('jsonwebtoken'); | ||
|
||
const Usuario = require('../models/usuario'); | ||
|
||
const app = express(); | ||
|
||
app.post('/login', (req,res) => { | ||
|
||
let body = req.body; | ||
|
||
Usuario.findOne({email: body.email}, (err, usuarioDB) => { | ||
|
||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
err | ||
}); | ||
} | ||
|
||
if(!usuarioDB){ | ||
return res.status(400).json({ | ||
ok: false, | ||
err: { | ||
message: '(Usuario) o contraseña incorrectos' | ||
} | ||
}); | ||
} | ||
|
||
if( !bcrypt.compareSync( body.password, usuarioDB.password) ){ | ||
return res.status(400).json({ | ||
ok: false, | ||
err: { | ||
message: 'Usuario o (contraseña) incorrectos' | ||
} | ||
}); | ||
} | ||
|
||
let token = jwt.sign({ | ||
usuario: usuarioDB | ||
}, process.env.SEED, { expiresIn: process.env.TOKEN_EXPIRE }) // 30 dias | ||
|
||
res.json({ | ||
ok: true, | ||
usuario: usuarioDB, | ||
token | ||
}); | ||
}); | ||
|
||
|
||
|
||
}); | ||
|
||
|
||
module.exports = app; |
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 |
---|---|---|
|
@@ -11,21 +11,22 @@ app.use(bodyParser.urlencoded({ extended: false})); | |
// parse application/json | ||
app.use(bodyParser.json()); | ||
|
||
// Incluyo archivo con las rutas del usuario | ||
app.use( require('./routes/usuario')); | ||
// Incluyo las rutas | ||
app.use(require('./routes/index')); | ||
|
||
//Conexion a DB | ||
// lucas: V7QZDXveXxwkst1l | ||
// mongodb+srv://lucas:[email protected]/cafe | ||
|
||
//console.log( process.env.URLDB ); | ||
|
||
mongoose.connect(process.env.URLDB, { useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true}, | ||
(err, res) => { | ||
mongoose.connect(process.env.URLDB, | ||
{ useNewUrlParser: true, useUnifiedTopology: true, useCreateIndex: true, useFindAndModify: false}, | ||
(err, res) => { | ||
|
||
if(err) throw new err; | ||
console.log('Base de datos ONLINE!'); | ||
if(err) throw new err; | ||
console.log('Base de datos ONLINE!'); | ||
}); | ||
|
||
|
||
app.listen(process.env.PORT, () => { console.log(`Escuchando el puerto ${process.env.PORT}`); }) | ||
app.listen(process.env.PORT, () => { console.log(`Escuchando el puerto ${ process.env.PORT }`); }) |