Login
This commit is contained in:
@@ -7,3 +7,7 @@ USERDB = censoDev
|
||||
PASSDB = holaC3ns0
|
||||
|
||||
HOSTDB = localhost
|
||||
|
||||
TOKEN_SEED = seedAcatlan
|
||||
|
||||
TOKEN_EXP = 1d
|
||||
Generated
+27
-4
@@ -823,8 +823,7 @@
|
||||
"graceful-fs": {
|
||||
"version": "4.2.4",
|
||||
"resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.4.tgz",
|
||||
"integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw==",
|
||||
"dev": true
|
||||
"integrity": "sha512-WjKPNJF79dtJAVniUlGGWHYGz2jWxT6VhN/4m1NdkbZ2nOsEF+cI1Edgql5zCRhs/VsQYRvrXctxktVXZUkixw=="
|
||||
},
|
||||
"has-flag": {
|
||||
"version": "3.0.0",
|
||||
@@ -899,8 +898,7 @@
|
||||
"imurmurhash": {
|
||||
"version": "0.1.4",
|
||||
"resolved": "https://registry.npmjs.org/imurmurhash/-/imurmurhash-0.1.4.tgz",
|
||||
"integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o=",
|
||||
"dev": true
|
||||
"integrity": "sha1-khi5srkoojixPcT7a21XbyMUU+o="
|
||||
},
|
||||
"inflection": {
|
||||
"version": "1.12.0",
|
||||
@@ -1390,6 +1388,26 @@
|
||||
"resolved": "https://registry.npmjs.org/node-addon-api/-/node-addon-api-3.0.0.tgz",
|
||||
"integrity": "sha512-sSHCgWfJ+Lui/u+0msF3oyCgvdkhxDbkCS6Q8uiJquzOimkJBvX6hl5aSSA7DR1XbMpdM8r7phjcF63sF4rkKg=="
|
||||
},
|
||||
"node-localstorage": {
|
||||
"version": "2.1.6",
|
||||
"resolved": "https://registry.npmjs.org/node-localstorage/-/node-localstorage-2.1.6.tgz",
|
||||
"integrity": "sha512-yE7AycE5G2hU55d+F7Ona9nx97C+enJzWWx6jrsji7fuPZFJOvuW3X/LKKAcXRBcEIJPDOKt8ZiFWFmShR/irg==",
|
||||
"requires": {
|
||||
"write-file-atomic": "^1.1.4"
|
||||
},
|
||||
"dependencies": {
|
||||
"write-file-atomic": {
|
||||
"version": "1.3.4",
|
||||
"resolved": "https://registry.npmjs.org/write-file-atomic/-/write-file-atomic-1.3.4.tgz",
|
||||
"integrity": "sha1-+Aek8LHZ6ROuekgRLmzDrxmRtF8=",
|
||||
"requires": {
|
||||
"graceful-fs": "^4.1.11",
|
||||
"imurmurhash": "^0.1.4",
|
||||
"slide": "^1.1.5"
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
"node-pre-gyp": {
|
||||
"version": "0.15.0",
|
||||
"resolved": "https://registry.npmjs.org/node-pre-gyp/-/node-pre-gyp-0.15.0.tgz",
|
||||
@@ -1891,6 +1909,11 @@
|
||||
"resolved": "https://registry.npmjs.org/signal-exit/-/signal-exit-3.0.3.tgz",
|
||||
"integrity": "sha512-VUJ49FC8U1OxwZLxIbTTrDvLnf/6TDgxZcK8wxR8zs13xpx7xbG60ndBlhNrFi2EMuFRoeDoJO7wthSLq42EjA=="
|
||||
},
|
||||
"slide": {
|
||||
"version": "1.1.6",
|
||||
"resolved": "https://registry.npmjs.org/slide/-/slide-1.1.6.tgz",
|
||||
"integrity": "sha1-VusCfWW00tzmyy4tMsTUr8nh1wc="
|
||||
},
|
||||
"sqlstring": {
|
||||
"version": "2.3.2",
|
||||
"resolved": "https://registry.npmjs.org/sqlstring/-/sqlstring-2.3.2.tgz",
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
"mariadb": "^2.4.2",
|
||||
"moment": "^2.27.0",
|
||||
"mysql2": "^2.1.0",
|
||||
"node-localstorage": "^2.1.6",
|
||||
"sequelize": "^6.3.4"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -0,0 +1 @@
|
||||
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjp7ImlkQWRtaW4iOjIsIm5hbWUiOiJhbGZyZWRvamwiLCJwYXNzd29yZCI6IiQyYiQxMCRlbXlCSGMxSUROWmpKcHU1aEJnVzB1QzJ1ZGZYbFJRY1NkSUhSVTZyNGNHOTZvOGdHa2p1UyIsImlzQWRtaW4iOmZhbHNlLCJjcmVhdGVkQXQiOiIyMDIwLTA4LTE1VDE3OjQ5OjA2LjAwMFoiLCJ1cGRhdGVkQXQiOiIyMDIwLTA4LTE1VDE3OjQ5OjA2LjAwMFoifSwiaWF0IjoxNTk3NjIxNDAzLCJleHAiOjE1OTc3MDc4MDN9.TjRxq5nn4EEYCu38BD_AeLbRVY6w6IeWCwNWhEgnS9U
|
||||
@@ -4,12 +4,12 @@ const sequelize = require("./sequelize.conf");
|
||||
const colors = require("colors");
|
||||
|
||||
sequelize
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
console.log("Conexión establecida con éxito.".green);
|
||||
process.exit();
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("No se ha podido establacer conexión con la base de datos:".red, err);
|
||||
process.exit();
|
||||
});
|
||||
.authenticate()
|
||||
.then(() => {
|
||||
console.log("Conexion establecida con exito.".green);
|
||||
process.exit();
|
||||
})
|
||||
.catch(err => {
|
||||
console.error("No se ha podido establacer conexion con la base de datos:".red, err);
|
||||
process.exit();
|
||||
});
|
||||
@@ -1,15 +1,54 @@
|
||||
// const post = async () => {
|
||||
|
||||
// return {
|
||||
// message: 'Mensaje del post'
|
||||
// }
|
||||
// }
|
||||
|
||||
// module.exports = post;
|
||||
|
||||
const Admin = require("../../models/tablas/Admin");
|
||||
const bcrypt = require("bcrypt");
|
||||
|
||||
const jwt = require("jsonwebtoken");
|
||||
|
||||
// module.exports = const equis;
|
||||
|
||||
const post = async(req, res) => {
|
||||
|
||||
const body = req.body;
|
||||
|
||||
const user = await Admin.findOne({
|
||||
where: {
|
||||
name: body.name,
|
||||
},
|
||||
});
|
||||
|
||||
if (user == null) {
|
||||
console.log("not found!");
|
||||
return res.status(500).json({
|
||||
err: {
|
||||
message: `No se pudo encontrar «${body.name}»`
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (!bcrypt.compareSync(body.password, user.password)) {
|
||||
console.log('Wrong password');
|
||||
return res.json({
|
||||
err: {
|
||||
message: 'Wrong password'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
let token = jwt.sign({
|
||||
user
|
||||
}, process.env.TOKEN_SEED, { expiresIn: process.env.TOKEN_EXP });
|
||||
|
||||
if (typeof localStorage === "undefined" || localStorage === null) {
|
||||
var LocalStorage = require('node-localstorage').LocalStorage;
|
||||
localStorage = new LocalStorage('./scratch');
|
||||
}
|
||||
localStorage.setItem('token', token);
|
||||
|
||||
|
||||
res.json({
|
||||
user,
|
||||
token
|
||||
})
|
||||
|
||||
|
||||
}
|
||||
|
||||
module.exports = post;
|
||||
@@ -0,0 +1,28 @@
|
||||
const jwt = require('jsonwebtoken');
|
||||
|
||||
const verifyToken = (req, res, next) => {
|
||||
|
||||
if (typeof localStorage === "undefined" || localStorage === null) {
|
||||
var LocalStorage = require('node-localstorage').LocalStorage;
|
||||
localStorage = new LocalStorage('./scratch');
|
||||
}
|
||||
let localToken = localStorage.getItem('token');
|
||||
|
||||
console.log(localToken);
|
||||
|
||||
jwt.verify(localToken, process.env.TOKEN_SEED, (err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(401).json({
|
||||
err: {
|
||||
message: 'Invalid token.'
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
req.user = decoded.user;
|
||||
|
||||
next();
|
||||
})
|
||||
}
|
||||
|
||||
module.exports = verifyToken;
|
||||
+40
-19
@@ -7,35 +7,56 @@ const admin = require("./tablas/Admin");
|
||||
require("../config/config");
|
||||
|
||||
async function remove() {
|
||||
console.log(logSymbols.info, "Paso 1 : Borrando base de datos \n".blue.bold);
|
||||
console.log(logSymbols.info, "Paso 1 : Borrando base de datos \n".blue.bold);
|
||||
|
||||
await admin.drop();
|
||||
console.log(logSymbols.success, "admin");
|
||||
await admin.drop();
|
||||
console.log(logSymbols.success, "admin");
|
||||
|
||||
}
|
||||
|
||||
async function install() {
|
||||
console.log(
|
||||
logSymbols.info,
|
||||
"Paso 2 : Instalando base de datos \n".blue.bold
|
||||
);
|
||||
console.log(
|
||||
logSymbols.info,
|
||||
"Paso 2 : Instalando base de datos \n".blue.bold
|
||||
);
|
||||
|
||||
await admin.sync();
|
||||
console.log(logSymbols.success, "admin");
|
||||
await admin.sync();
|
||||
console.log(logSymbols.success, "admin");
|
||||
|
||||
//crear clave foranea de una tabla a otra, ejemplo
|
||||
// responsable.hasMany(programa, {
|
||||
// foreignKey: "idResponsable",
|
||||
// });
|
||||
|
||||
};
|
||||
|
||||
async function createFirstUsers() {
|
||||
console.log(
|
||||
logSymbols.info,
|
||||
"Paso 3 : Creating first users in database...\n".blue.bold
|
||||
);
|
||||
|
||||
await admin.create({
|
||||
name: 'alima',
|
||||
password: bcrypt.hashSync('contraseña', 10),
|
||||
isAdmin: true
|
||||
});
|
||||
|
||||
await admin.create({
|
||||
name: 'alfredojl',
|
||||
password: bcrypt.hashSync('contraseña', 10),
|
||||
isAdmin: false
|
||||
});
|
||||
|
||||
//crear clave foranea de una tabla a otra, ejemplo
|
||||
// responsable.hasMany(programa, {
|
||||
// foreignKey: "idResponsable",
|
||||
// });
|
||||
|
||||
}
|
||||
|
||||
async function exec() {
|
||||
await remove();
|
||||
await install();
|
||||
await remove();
|
||||
await install();
|
||||
await createFirstUsers();
|
||||
|
||||
console.log(logSymbols.success, "Éxito al instalar \n".underline.bold.green);
|
||||
process.exit();
|
||||
console.log(logSymbols.success, "Éxito al instalar \n".underline.bold.green);
|
||||
process.exit();
|
||||
}
|
||||
|
||||
exec();
|
||||
exec();
|
||||
@@ -2,27 +2,28 @@ const Sequelize = require("sequelize");
|
||||
const sequelize = require("../../config/sequelize.conf");
|
||||
|
||||
class Admin extends Sequelize.Model {}
|
||||
Admin.init(
|
||||
{
|
||||
Admin.init({
|
||||
idAdmin: {
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false,
|
||||
type: Sequelize.INTEGER,
|
||||
primaryKey: true,
|
||||
autoIncrement: true,
|
||||
allowNull: false,
|
||||
},
|
||||
nombre: {
|
||||
type: Sequelize.STRING(50),
|
||||
allowNull: false,
|
||||
name: {
|
||||
type: Sequelize.STRING(50),
|
||||
allowNull: false,
|
||||
},
|
||||
password: {
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: false,
|
||||
type: Sequelize.STRING(100),
|
||||
allowNull: false,
|
||||
},
|
||||
},
|
||||
{
|
||||
isAdmin: {
|
||||
type: Sequelize.BOOLEAN,
|
||||
allowNull: false
|
||||
}
|
||||
}, {
|
||||
sequelize,
|
||||
tableName: "admin",
|
||||
}
|
||||
);
|
||||
tableName: "users",
|
||||
});
|
||||
|
||||
module.exports = Admin;
|
||||
module.exports = Admin;
|
||||
+6
-26
@@ -1,38 +1,18 @@
|
||||
let express = require("express");
|
||||
let app = express();
|
||||
let verifyToken = require('../middlewares/authentication');
|
||||
|
||||
const route = '/login'
|
||||
|
||||
//logica del login, ejemplo
|
||||
let post = require('../controller/login/post')
|
||||
let post = require('../controller/login/post');
|
||||
const Admin = require("../models/tablas/Admin");
|
||||
//let get = require('../controller/login/get')
|
||||
|
||||
app.post(route, (req, res) => {
|
||||
console.log(req.query)
|
||||
console.log(req.body)
|
||||
return post()
|
||||
.then(data => {
|
||||
res.status(200).json(data)
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err)
|
||||
res.status(400).json({
|
||||
err: err.message
|
||||
})
|
||||
})
|
||||
})
|
||||
app.post(route, async(req, res) => {
|
||||
|
||||
return post(req, res);
|
||||
|
||||
// app.get(route, (req, res) => {
|
||||
// return get()
|
||||
// .then(data => {
|
||||
// res.status(200).json(data)
|
||||
// })
|
||||
// .catch(err => {
|
||||
// res.status(400).json({
|
||||
// err: err.message
|
||||
// })
|
||||
// })
|
||||
// })
|
||||
});
|
||||
|
||||
module.exports = app
|
||||
Reference in New Issue
Block a user