integracion auth x token
This commit is contained in:
@@ -30,7 +30,7 @@ controlador.app_login = (req, res) => {
|
|||||||
controlador.auth = (req, res) => {
|
controlador.auth = (req, res) => {
|
||||||
console.log(req.body);
|
console.log(req.body);
|
||||||
var pwd = stringTo_md5(req.body.password);
|
var pwd = stringTo_md5(req.body.password);
|
||||||
req.getConnection((error, conn,next) => {
|
req.getConnection((error, conn, next) => {
|
||||||
conn.query(`SELECT * FROM usuarios WHERE n_sesion = ? and clave=?`, [req.body.usuario, pwd], (err, rows) => {
|
conn.query(`SELECT * FROM usuarios WHERE n_sesion = ? and clave=?`, [req.body.usuario, pwd], (err, rows) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
//res.json(err);
|
//res.json(err);
|
||||||
@@ -66,50 +66,32 @@ controlador.login_test = (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
async function keyGen(req,id) {
|
|
||||||
//var id = sql.consulta_valorCampoString("empresa_datos", "RUC", "idEmpresa", "1");
|
|
||||||
await req.getConnection(async(error, conn,next) => {
|
|
||||||
await conn.query(`SELECT RUC FROM empresa_datos WHERE idEmpresa = ? `, [id], (err, rows) => {
|
|
||||||
if (err) {
|
|
||||||
//res.json(err);
|
|
||||||
console.log("error en la consulta: ",err);
|
|
||||||
} else {
|
|
||||||
try {
|
|
||||||
if (rows.length > 0) {
|
|
||||||
console.log(rows[0].RUC);
|
|
||||||
return base64encode(rows[0].RUC);
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
|
||||||
console.log(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
//conn.end();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
//************* GENERA TOKENS ****************//
|
//************* GENERA TOKENS ****************//
|
||||||
controlador.auth_keygen = (req, res) => {
|
controlador.auth_keygen = async (req, res) => {
|
||||||
const datUsr={id,n_sesion,inf,dvc,app,usr, pwd, mail} = base64decode(req.body.data);
|
var datUsr = base64decode(req.body.data);
|
||||||
console.log(datUsr);
|
const jsonDat = JSON.parse(datUsr.replaceAll("'", "\"")); // Convertir a JSON
|
||||||
var pasw = "7c0a2d79657d70089926fe01aebf4d6f";//stringTo_md5(datUsr.pwd);
|
console.log("Json Obtenido: ", jsonDat);
|
||||||
req.getConnection((error, conn,next) => {
|
jsonDat.pwd = stringTo_md5(jsonDat.pwd);
|
||||||
conn.query(`select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = ? and u.n_sesion = ? and u.clave = ? `, [1,usr,pasw], (err, rows) => {
|
//const consulta = `select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = 1 and u.n_sesion = ${json.usr} and u.clave = ${json.pwd}`;
|
||||||
//conn.query(`select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = ? and u.cod_usr = 'XU0001' `, [1,id], (err, rows) => {
|
//console.log(consulta);
|
||||||
if (err) {
|
await req.getConnection(async (error, conn, next) => {
|
||||||
//res.json(err);
|
await conn.query(`select u.nombre,u.apellido,e.RUC from usuarios as u,empresa_datos as e where idEmpresa = ? and u.n_sesion = ? and u.clave = ? `, [1, jsonDat.usr, jsonDat.pwd], (errSql, rows) => {
|
||||||
console.log("error en la consulta: ",err);
|
if (errSql) {
|
||||||
|
console.log("error en la consulta: ", errSql);
|
||||||
} else {
|
} else {
|
||||||
try {
|
try {
|
||||||
if (rows.length > 0) {
|
if (rows.length > 0) {
|
||||||
|
delete jsonDat.pwd;
|
||||||
console.log(rows[0].RUC);
|
console.log(rows[0].RUC);
|
||||||
var key = base64encode(rows[0].RUC);
|
var key = base64encode(rows[0].RUC);
|
||||||
var nom = rows[0].nombre;
|
var nom = rows[0].nombre;
|
||||||
var ape = rows[0].apellido;
|
var ape = rows[0].apellido;
|
||||||
const token = jwt.sign(datUsr, config.secret.key);
|
const token = jwt.sign(jsonDat, config.secret.key);
|
||||||
var auth = {auth:[{"apellido":ape,"nombre":nom,"key":key,token:token}]};
|
var auth = { auth: [{ "apellido": ape, "nombre": nom, "key": key, token: token }] };
|
||||||
|
//console.log(auth);
|
||||||
res.json(auth);
|
res.json(auth);
|
||||||
}else{
|
} else {
|
||||||
res.json({auth:false, message:'Unauthorized'});
|
res.json({ auth: false, message: 'Unauthorized' });
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.log(ex);
|
console.log(ex);
|
||||||
@@ -117,27 +99,42 @@ controlador.auth_keygen = (req, res) => {
|
|||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
};
|
};
|
||||||
|
//************* valida TOKENS ****************//
|
||||||
|
controlador.auth_token = async (req, res) => {
|
||||||
|
console.log(req.body.data);
|
||||||
|
var token = req.body.data;
|
||||||
|
if (!token) {
|
||||||
|
return res.status(401).json({ auth: false, message: 'No token provided' });
|
||||||
|
}
|
||||||
|
try{
|
||||||
|
var decoded = jwt.verify(token, config.secret.key);
|
||||||
|
res.json(decoded);
|
||||||
|
}catch(err){
|
||||||
|
res.json({ auth: false, message: 'Unauthorized: '+err.message });
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
//************* INIT APP ****************//
|
||||||
controlador.init_app = (req, res) => {
|
controlador.init_app = (req, res) => {
|
||||||
console.log(req.body);
|
console.log(req.body);
|
||||||
//const datUsr={usr, pwd, mail} = req.body;
|
//const datUsr={usr, pwd, mail} = req.body;
|
||||||
//const token = jwt.sign(datUsr, config.secret.key);
|
//const token = jwt.sign(datUsr, config.secret.key);
|
||||||
//res.render(token);
|
//res.render(token);
|
||||||
res.json({token:token});
|
res.json({ token: token });
|
||||||
};
|
};
|
||||||
//************* LOGIN APP ****************//
|
//************* LOGIN APP ****************//
|
||||||
controlador.login_appTK = (req, res) => {
|
controlador.login_appTK = (req, res) => {
|
||||||
const token = req.headers['x-access-token'];
|
const token = req.headers['x-access-token'];
|
||||||
console.log("token: ",token);
|
console.log("token: ", token);
|
||||||
if(!token){
|
if (!token) {
|
||||||
return res.status(401).json({auth:false, message:'No token provided'});
|
return res.status(401).json({ auth: false, message: 'No token provided' });
|
||||||
}
|
}
|
||||||
jwt.verify(token, config.secret, (err,decoded) => {
|
jwt.verify(token, config.secret, (err, decoded) => {
|
||||||
if(err){
|
if (err) {
|
||||||
return res.status(401).json({auth:false, message:'Unauthorized'});
|
return res.status(401).json({ auth: false, message: 'Unauthorized' });
|
||||||
}
|
}
|
||||||
res.json({auth:true, message:'Authorized'});
|
res.json({ auth: true, message: 'Authorized' });
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -215,12 +212,12 @@ controlador.recibe_pedidos = async (req, res) => {
|
|||||||
PedUsoPrdct_iva: json.iva,
|
PedUsoPrdct_iva: json.iva,
|
||||||
PedUsoPrdct_origen: json.origen
|
PedUsoPrdct_origen: json.origen
|
||||||
};
|
};
|
||||||
ingreso_pedido(req,pedido,json.items);
|
ingreso_pedido(req, pedido, json.items);
|
||||||
//await console.log(await ingreso_pedido(req,pedido));
|
//await console.log(await ingreso_pedido(req,pedido));
|
||||||
//await ingreso_detalle(req, id_ped, json.items);
|
//await ingreso_detalle(req, id_ped, json.items);
|
||||||
};
|
};
|
||||||
|
|
||||||
async function ingreso_pedido(req,pedido_data,items) {
|
async function ingreso_pedido(req, pedido_data, items) {
|
||||||
var lastInsert = "";
|
var lastInsert = "";
|
||||||
req.getConnection((err, conn) => {
|
req.getConnection((err, conn) => {
|
||||||
conn.query('INSERT INTO PedidoUsoProduct set ?', [pedido_data], (err, result) => {
|
conn.query('INSERT INTO PedidoUsoProduct set ?', [pedido_data], (err, result) => {
|
||||||
@@ -230,7 +227,7 @@ async function ingreso_pedido(req,pedido_data,items) {
|
|||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
lastInsert = result.insertId;
|
lastInsert = result.insertId;
|
||||||
console.log("existe id = "+lastInsert);
|
console.log("existe id = " + lastInsert);
|
||||||
ingreso_detalle(req, lastInsert, items)
|
ingreso_detalle(req, lastInsert, items)
|
||||||
//res.json({ sision: "ORDEN INGRESADA" });
|
//res.json({ sision: "ORDEN INGRESADA" });
|
||||||
//res.redirect('/clientes');//redireciona a la ruta inical de la app
|
//res.redirect('/clientes');//redireciona a la ruta inical de la app
|
||||||
@@ -242,14 +239,14 @@ async function ingreso_detalle(req, id_ped, data) {
|
|||||||
//console.log("ID Pedido: "+id_ped);
|
//console.log("ID Pedido: "+id_ped);
|
||||||
for (var i = 0; i < data.length; i++) {
|
for (var i = 0; i < data.length; i++) {
|
||||||
var items = {
|
var items = {
|
||||||
PedUsoPrdct_id:id_ped,
|
PedUsoPrdct_id: id_ped,
|
||||||
//PedUsoPrdct_id: 36,
|
//PedUsoPrdct_id: 36,
|
||||||
PedUsoPrdctDet_codigoProducto: data[i].cod,
|
PedUsoPrdctDet_codigoProducto: data[i].cod,
|
||||||
PedUsoPrdct_cant: data[i].cant,
|
PedUsoPrdct_cant: data[i].cant,
|
||||||
PedUsoPrdct_costo: data[i].precio,
|
PedUsoPrdct_costo: data[i].precio,
|
||||||
PedUsoPrdct_desct: data[i].descuento,
|
PedUsoPrdct_desct: data[i].descuento,
|
||||||
//PedUsoPrdct_iva:data[i].iva,
|
//PedUsoPrdct_iva:data[i].iva,
|
||||||
PedUsoPrdct_iva:0,
|
PedUsoPrdct_iva: 0,
|
||||||
PedUsoPrdct_gpPrecios: data[i].gp_precio
|
PedUsoPrdct_gpPrecios: data[i].gp_precio
|
||||||
}
|
}
|
||||||
//console.log(items);
|
//console.log(items);
|
||||||
|
|||||||
@@ -17,12 +17,14 @@ rutas.post('/login', controlador_init.auth);//Authenticacion de Web APP
|
|||||||
rutas.get('/login_test', controlador_init.login_test);//login testing css / dev
|
rutas.get('/login_test', controlador_init.login_test);//login testing css / dev
|
||||||
|
|
||||||
rutas.get('/init-app', controlador_init.init_app);//init app / dev
|
rutas.get('/init-app', controlador_init.init_app);//init app / dev
|
||||||
rutas.post('/auth-keygen', controlador_init.auth_keygen);//genera TOKENS / dev
|
|
||||||
rutas.post('/login_app', controlador_init.login_appTK);//login APP / dev
|
rutas.post('/login_app', controlador_init.login_appTK);//login APP / dev
|
||||||
|
|
||||||
|
rutas.post('/auth-keygen', controlador_init.auth_keygen);//genera TOKENS / dev
|
||||||
|
rutas.post('/auth-token', controlador_init.auth_token);//Valida TOKENS / dev
|
||||||
|
|
||||||
rutas.post('/operaciones/', controlador_init.recibe_datos);//testing json reccepcion server
|
rutas.post('/operaciones/', controlador_init.recibe_datos);//testing json reccepcion server
|
||||||
|
|
||||||
rutas.get('/app-tv/', controlador_init.app_tv);//app de tv y video json reccepcion server
|
rutas.get('/app-tv/', controlador_init.app_tv);//app de tv y video json
|
||||||
rutas.get('/tv-online/', controlador_init.app_tv);//app de tv y video json reccepcion server
|
rutas.get('/tv-online/', controlador_init.app_tv);//app de tv y video json
|
||||||
|
|
||||||
module.exports = rutas;
|
module.exports = rutas;
|
||||||
|
|||||||
Reference in New Issue
Block a user