-
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
401c5bf
commit fd1cb70
Showing
9 changed files
with
409 additions
and
9 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 |
---|---|---|
|
@@ -6,19 +6,29 @@ var port = process.env.PORT || 3000; */ | |
// requires | ||
var express = require('express'); | ||
const mongoose = require("mongoose"); | ||
var bodyParser = require('body-parser'); | ||
|
||
// Inicializar variables | ||
var app = express(); | ||
|
||
// Body Parser | ||
// parse application/x-www-form-urlencoded | ||
app.use(bodyParser.urlencoded({ extended: false })); | ||
app.use(bodyParser.json()); | ||
|
||
// mongo cloud | ||
const port = process.env.PORT || 3000; | ||
const MC_url = "mongodb+srv://pablo:[email protected]/backend"; | ||
const MC_url = "mongodb+srv://pablo:[email protected]/hospitalDB"; | ||
|
||
// Importar rutas | ||
var appRoutes = require('./routes/app'); | ||
var usuarioRoutes = require('./routes/usuario'); | ||
var loginRoutes = require('./routes/login'); | ||
|
||
app.use('/usuario', usuarioRoutes); | ||
app.use('/login', loginRoutes); | ||
app.use('/', appRoutes); | ||
|
||
// Rutas | ||
app.get('/', (req, res, next) => { | ||
res.status(403).send({ | ||
ok: true, | ||
mensaje: 'Peticion realizada correctamente' | ||
}) | ||
}) | ||
|
||
mongoose.Promise = global.Promise; | ||
mongoose.connect(MC_url, { | ||
|
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@mi-seed-secreto'; |
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,28 @@ | ||
'use strict' | ||
'use esversion: 6 ' | ||
|
||
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).send({ | ||
ok: false, | ||
mensaje: 'Token incorrecto', | ||
errors: err | ||
}); | ||
} | ||
req.usuario = decoded.usuario; | ||
next(); | ||
}); | ||
} |
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,21 @@ | ||
var moongoose = require('mongoose'); | ||
var uniqueValidator = require('mongoose-unique-validator'); | ||
|
||
var Schema = moongoose.Schema; | ||
|
||
var rolesValidos = { | ||
values: ['ADMIN_ROLE', 'USER_ROLE'], | ||
message: '{VALUE} no es un rol permitido' | ||
} | ||
|
||
var usuarioSchema = new Schema({ | ||
'nombre': { type: String, required: [true, 'El nombre es obligatorio'] }, | ||
'correo': { type: String, unique: true, required: [true, 'El correo es obligatorio'] }, | ||
'clave': { type: String, required: [true, 'La clave de acceso es obligatorio'] }, | ||
'img': { type: String, required: [false] }, | ||
'role': { type: String, required: true, default: 'USER_ROLE', enum: rolesValidos } | ||
}); | ||
|
||
usuarioSchema.plugin(uniqueValidator, { message: "El campo {PATH}, debe ser unico" }); | ||
|
||
module.exports = moongoose.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,15 @@ | ||
'use strict' | ||
|
||
var express = require('express'); | ||
var app = express(); | ||
|
||
|
||
// Rutas | ||
app.get('/', (req, res, next) => { | ||
res.status(403).send({ | ||
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,54 @@ | ||
'use strict' | ||
|
||
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({ correo: body.correo }, (err, usuarioDB) => { | ||
if (err) { | ||
return res.status(500).send({ | ||
ok: false, | ||
mensaje: 'Error al buscar el usuario', | ||
errors: err | ||
}); | ||
} | ||
if (!usuarioDB) { | ||
return res.status(400).send({ | ||
ok: false, | ||
mensaje: 'Login incorrecto.', | ||
errors: { message: "# No existe un usuario con ese correo electronico" } | ||
}); | ||
} | ||
|
||
if (!bcrypt.compareSync(body.clave, usuarioDB.clave)) { | ||
return res.status(400).send({ | ||
ok: false, | ||
mensaje: 'Login incorrecto.', | ||
errors: { message: "# La clave es incorrecta" } | ||
}); | ||
} | ||
// La clave no se integra al token del usuario | ||
usuarioDB.clave = ':-;'; | ||
|
||
// Crear un token | ||
var token = jwt.sign({ usuario: usuarioDB }, SEED, { expiresIn: 14400 }); //4 horas | ||
|
||
return res.status(200).send({ | ||
ok: true, | ||
usuario: usuarioDB, | ||
token, | ||
id: usuarioDB.id | ||
}); | ||
}) | ||
|
||
}); | ||
|
||
|
||
module.exports = app; |
Oops, something went wrong.