-
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
1 parent
e67833b
commit 59c1e70
Showing
9 changed files
with
432 additions
and
10 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
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 @@ | ||
module.exports.SEED = '@este-es@-un-seed-absurdo'; |
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,25 @@ | ||
var jwt = require('jsonwebtoken'); | ||
var SEED = require('../config/config').SEED; | ||
|
||
// ========================== | ||
// Verificar token | ||
// ========================== | ||
exports.verificaToken = function(req, res, next) { | ||
var token = req.query.token; | ||
|
||
jwt.verify(token, SEED, (err, decoded) => { | ||
if (err) { | ||
return res.status(401).json({ | ||
ok: false, | ||
mensaje: 'Token no valido', | ||
errors: err | ||
}); | ||
} | ||
req.usuario = decoded.usuario; | ||
next(); | ||
// res.status(200).json({ | ||
// ok: true, | ||
// decode: decoded | ||
// }); | ||
}) | ||
} |
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,44 @@ | ||
var mongoose = require('mongoose'); | ||
var uniqueValidator = require('mongoose-unique-validator'); | ||
|
||
var Schema = mongoose.Schema; | ||
|
||
|
||
var rolesValidos = { | ||
values: ['ADMIN_ROLE', 'USER_ROLE'], | ||
message: '{VALUE} no es un rol valido' | ||
}; | ||
|
||
var usuarioSchema = new Schema({ | ||
|
||
nombre: { | ||
type: String, | ||
required: [true, 'El nombre es obligatorio'] | ||
}, | ||
|
||
email: { | ||
type: String, | ||
unique: true, | ||
required: [true, 'El email es obligatorio'] | ||
}, | ||
|
||
password: { | ||
type: String, | ||
required: [true, 'La contraseña es obligatoria'] | ||
}, | ||
|
||
img: { | ||
type: String, | ||
required: false | ||
}, | ||
|
||
role: { | ||
type: String, | ||
required: true, | ||
default: 'USER_ROLE', | ||
enum: rolesValidos, | ||
} | ||
}); | ||
|
||
usuarioSchema.plugin(uniqueValidator, { message: '{PATH} debe ser único' }) | ||
module.exports = mongoose.model('Usuario', usuarioSchema); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
var express = require('express'); | ||
|
||
var app = express(); | ||
|
||
app.get('/', (req, res, next) => { | ||
|
||
res.status(200).json({ | ||
ok: true, | ||
mensaje: 'Peticion realizada correctamente' | ||
}) | ||
}); | ||
|
||
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,61 @@ | ||
var express = require('express'); | ||
var bcrypt = require('bcryptjs'); | ||
var jwt = require('jsonwebtoken'); | ||
|
||
var SEED = require('../config/config').SEED; | ||
|
||
var app = express(); | ||
|
||
var Usuario = require('../models/usuario'); | ||
|
||
|
||
app.post('/', (req, res) => { | ||
|
||
var body = req.body; | ||
|
||
Usuario.findOne({ email: body.email }, (err, usuarioBD) => { | ||
if (err) { | ||
return res.status(500).json({ | ||
ok: false, | ||
mensaje: 'Error al buscar usuario', | ||
errors: err | ||
}); | ||
} | ||
|
||
// Evaluar si existe ese ususario | ||
|
||
if (!usuarioBD) { | ||
return res.status(400).json({ | ||
ok: false, | ||
// TODO: quitar -email, es solo para comprbar que falla en desarrollo | ||
mensaje: 'Credenciales incorrectas - email', | ||
errors: err | ||
}); | ||
} | ||
|
||
//Validamos que la contraseña sea correcta | ||
if (!bcrypt.compareSync(body.password, usuarioBD.password)) { | ||
return res.status(400).json({ | ||
ok: false, | ||
mensaje: 'Credenciales incorrectas - password', | ||
errors: err | ||
}); | ||
} | ||
|
||
//Crear un token | ||
usuarioBD.password = ':)'; | ||
|
||
var token = jwt.sign({ usuario: usuarioBD }, SEED, { expiresIn: 14400 }) //4 horas | ||
|
||
res.status(200).json({ | ||
ok: true, | ||
usuario: usuarioBD, | ||
token: token, | ||
id: usuarioBD.id | ||
}) | ||
|
||
}); | ||
|
||
}); | ||
|
||
module.exports = app; |
Oops, something went wrong.