integracion auth x token
This commit is contained in:
@@ -66,47 +66,29 @@ 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' });
|
||||||
@@ -117,8 +99,23 @@ 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;
|
||||||
|
|||||||
@@ -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