Last active
December 30, 2018 16:46
-
-
Save sarmanulco/49e582ab0317e1c542f30acf8d1f69eb to your computer and use it in GitHub Desktop.
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
INSERT INTO Users (first_name, last_name, email, pass) values (?,?,?,?) |
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
SELECT * FROM Users WHERE email = ? |
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
const mysql = require('mysql') | |
const queries = require('./queries') | |
var connection = mysql.createConnection({ | |
host : 'localhost', | |
user : 'OOOOOOOO', | |
password : 'OOOOOOOO', | |
database : 'OOOOOOOO' | |
}); | |
connection.pQuery = (query, values = []) => new Promise((resolve, reject) => { | |
const sql = queries[query] | |
connection.query(sql, values, (error, results, fields) => { | |
if (error) { | |
return reject(error) | |
} | |
resolve({results, fields}) | |
}) | |
}) | |
module.exports = connection |
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
const conn = use('dbConnection') | |
var bcrypt = require('bcrypt'); | |
var BCRYPT_SALT_ROUNDS = 12; | |
module.exports = { | |
async register (req, res, next) { | |
const {name, surname, email, pass} = req.body | |
const encrypedPass = await bcrypt.hash(pass, BCRYPT_SALT_ROUNDS) | |
try { | |
await conn.pQuery('register-user', [name, surname, email, encrypedPass]) | |
} catch (error) { | |
next(error) | |
} | |
res.locals = {status: 201, message: 'Usuario creado'} | |
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
const conn = use('dbConnection') // con la function use recupero conexión con la base de datos | |
var jwt = require('jsonwebtoken'); | |
var bcrypt = require('bcrypt'); | |
module.exports = { | |
async login (req, res, next) { | |
const {email, pass} = req.body | |
let user | |
try { | |
const selectUser = await conn.pQuery('select-user', [email]) | |
user = selectUser.results && selectUser.results[0] | |
if (!user) { | |
res.locals = {status: 403, message: 'Usuario incorrecto'} | |
return next() | |
} | |
const passIsEqual = await bcrypt.compare(pass, user.pass); | |
console.log(pass, user.pass) | |
if (!passIsEqual) { | |
res.locals = {status: 403, message: 'Usuario incorrecto 2'} | |
return next() | |
} | |
} catch (error) { | |
return next(error) | |
} | |
delete user.pass | |
delete user.user_id | |
const cert = require('fs').readFileSync('certs/localhost.pem').toString() | |
const token = jwt.sign(user, cert) | |
res.locals = {status: 201, message: res.json(token)} | |
next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment