From 1b65d4af33714d2196e3ee64f93525f00b9d2e43 Mon Sep 17 00:00:00 2001 From: Fernando Date: Tue, 30 Oct 2018 10:20:02 -0500 Subject: [PATCH] Finalizada gestion de usuarios con google. --- Backend/start.ts | 25 ++++++++++-------- public/back/Inicio-Registro.js | 0 src/views/Administracion/Inicio.vue | 13 +++++++++ src/views/Login-Registro.vue | 8 ++++-- src/views/MiCuenta.vue | 41 ++++++++++++++++++++++------- src/views/carrito.vue | 2 +- 6 files changed, 66 insertions(+), 23 deletions(-) delete mode 100644 public/back/Inicio-Registro.js create mode 100644 src/views/Administracion/Inicio.vue diff --git a/Backend/start.ts b/Backend/start.ts index c343ae8..5ddf51e 100644 --- a/Backend/start.ts +++ b/Backend/start.ts @@ -10,9 +10,9 @@ const validator = require('./google_validation.js').validator; const crearUsuario = (con:any, data:object) => { return new Promise((resolve, reject) => { - con.query (`INSERT INTO clientes (nombre, email, img, carritoActual, historial)\ + con.query (`INSERT INTO usuarios (nombre, email, img, carritoActual, GOOGLE_ID)\ VALUES ( '${(data as any)["nombreUsuario"]}', '${(data as any)["emailUsuario"]}', - '${(data as any)["imagenUsuario"]}', '{}', '{}')`, + '${(data as any)["imagenUsuario"]}', '{}', '${(data as any)["googleID"]}')`, (err:any, res:string) => { if (err) { reject("Error al ejecutar query SQL (start.ts fila 18))...\n" + err); @@ -23,7 +23,7 @@ const crearUsuario = (con:any, data:object) => { }); }; -const revisarUsuarioBD = (usuario:object) => { +const revisarUsuarioG = (usuario:object) => { return new Promise((resolve, reject) => { const mysql = require('mysql'); @@ -42,24 +42,26 @@ const revisarUsuarioBD = (usuario:object) => { const nombreUsuario:string = (usuario as any)["name"]; const emailUsuario:string = (usuario as any)["email"]; const imagenUsuario:string = (usuario as any)["picture"]; + const googleID:number = (usuario as any)["sub"]; - con.query (`SELECT nombre, email, img, carritoActual, historial FROM clientes WHERE nombre='${nombreUsuario}'`, + con.query (`SELECT nombre, email, img, carritoActual, USER_ID FROM usuarios WHERE GOOGLE_ID='${googleID}'`, (err:any, response:any) => { if (err) { reject("Error al ejecutar query SQL"); } const respuestaBD:string = JSON.stringify(response); if (respuestaBD === '[]') { - crearUsuario(con, {nombreUsuario, emailUsuario, imagenUsuario}) + crearUsuario(con, {nombreUsuario, emailUsuario, imagenUsuario, googleID}) .then( () => { resolve(`{ "nombre": "${nombreUsuario}", "email": "${emailUsuario}", "img": "${imagenUsuario}", - "carrito": {}, "historial": {}}`); + "carrito": {}`); }) .catch((msg:string) => { reject("Error al crear el usuario...\n" + msg); }) } else { - resolve(respuestaBD); + console.log(`Respuesta servidor:\n${respuestaBD}`); + resolve(JSON.stringify(response[0])); } }); }); @@ -67,7 +69,7 @@ const revisarUsuarioBD = (usuario:object) => { }); }; -app.post('/log-in',(req:any, res:any) => { +app.post('/log-inG',(req:any, res:any) => { res.header('Access-Control-Allow-Origin', 'http://localhost:8080'); res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE'); res.header('Access-Control-Allow-Headers', 'Content-Type'); @@ -75,11 +77,12 @@ app.post('/log-in',(req:any, res:any) => { const token:string = req.body.idtoken; validator(token) - .then( (usuario:object) => - revisarUsuarioBD(usuario) + .then( (usuario:object) => { + revisarUsuarioG(usuario) .then(response => res.send(response.toString())) .catch(err => res.send(err)) - ) + + }) .catch(console.error); }); diff --git a/public/back/Inicio-Registro.js b/public/back/Inicio-Registro.js deleted file mode 100644 index e69de29..0000000 diff --git a/src/views/Administracion/Inicio.vue b/src/views/Administracion/Inicio.vue new file mode 100644 index 0000000..81aa11e --- /dev/null +++ b/src/views/Administracion/Inicio.vue @@ -0,0 +1,13 @@ + + + + + \ No newline at end of file diff --git a/src/views/Login-Registro.vue b/src/views/Login-Registro.vue index bbcea36..d73801d 100644 --- a/src/views/Login-Registro.vue +++ b/src/views/Login-Registro.vue @@ -68,6 +68,10 @@ const procesarUsuario = data => { const obj = JSON.parse(data); + obj["logged"] = true; + console.log(JSON.stringify(obj)); + window.usuarioActual.pop(); + window.usuarioActual.push(obj); window.elRouter.push('/platos/'); }; @@ -75,10 +79,10 @@ let id_token = googleUser.getAuthResponse().id_token; let xhr = new XMLHttpRequest(); - xhr.open('POST', 'http://localhost:3000/log-in'); + xhr.open('POST', 'http://localhost:3000/log-inG'); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded'); xhr.onload = function() { - console.log('Sesión iniciada.'); + console.log('Sesión iniciada. Servidor:\n' + xhr.responseText); procesarUsuario(xhr.responseText); }; xhr.send('idtoken=' + id_token); diff --git a/src/views/MiCuenta.vue b/src/views/MiCuenta.vue index c5f5979..401e4a7 100644 --- a/src/views/MiCuenta.vue +++ b/src/views/MiCuenta.vue @@ -27,11 +27,25 @@
- Hola {{ usuario[0].nombre }}, esta es toda la información que guardamos de ti:
+
+ Imagen del Usuario +
+ {{ usuario.nombre }}, desde aquí puedes acceder a todos tus datos, y modificarlos + si deseas. +
+
+

- Tu nombre: {{ usuario[0].nombre }}
- Tu email: {{ usuario[0].email }}
- +
+
+ +
+ +
+ + +
+
Tu historial de Compras
@@ -40,7 +54,7 @@ Tu historial de compras está vacio. ¡Compra algo y lo verás aquí!
-