diff --git a/package-lock.json b/package-lock.json index 18806ec..3440e1f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22,6 +22,11 @@ "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", "dev": true }, + "base64url": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/base64url/-/base64url-2.0.0.tgz", + "integrity": "sha1-6sFuA+oUOO/5Qj1puqNiYu0fcLs=" + }, "bcrypt": { "version": "2.0.1", "resolved": "https://registry.npmjs.org/bcrypt/-/bcrypt-2.0.1.tgz", @@ -454,6 +459,11 @@ "concat-map": "0.0.1" } }, + "buffer-equal-constant-time": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/buffer-equal-constant-time/-/buffer-equal-constant-time-1.0.1.tgz", + "integrity": "sha1-+OcRMvf/5uAaXJaXpMbz5I1cyBk=" + }, "buffer-writer": { "version": "1.0.1", "resolved": "https://registry.npmjs.org/buffer-writer/-/buffer-writer-1.0.1.tgz", @@ -512,6 +522,15 @@ "integrity": "sha1-rOb/gIwc5mtX0ev5eXessCM0z8E=", "dev": true }, + "ecdsa-sig-formatter": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/ecdsa-sig-formatter/-/ecdsa-sig-formatter-1.0.9.tgz", + "integrity": "sha1-S8kmJ07Dtau1AW5+HWCSGsJisqE=", + "requires": { + "base64url": "2.0.0", + "safe-buffer": "5.1.1" + } + }, "env2": { "version": "2.2.0", "resolved": "https://registry.npmjs.org/env2/-/env2-2.2.0.tgz", @@ -685,12 +704,85 @@ "resolved": "https://registry.npmjs.org/js-string-escape/-/js-string-escape-1.0.1.tgz", "integrity": "sha1-4mJbrbwNZ8dTPp7cEGjFh65BN+8=" }, + "jsonwebtoken": { + "version": "8.2.1", + "resolved": "https://registry.npmjs.org/jsonwebtoken/-/jsonwebtoken-8.2.1.tgz", + "integrity": "sha512-l8rUBr0fqYYwPc8/ZGrue7GiW7vWdZtZqelxo4Sd5lMvuEeCK8/wS54sEo6tJhdZ6hqfutsj6COgC0d1XdbHGw==", + "requires": { + "jws": "3.1.4", + "lodash.includes": "4.3.0", + "lodash.isboolean": "3.0.3", + "lodash.isinteger": "4.0.4", + "lodash.isnumber": "3.0.3", + "lodash.isplainobject": "4.0.6", + "lodash.isstring": "4.0.1", + "lodash.once": "4.1.1", + "ms": "2.1.1", + "xtend": "4.0.1" + } + }, + "jwa": { + "version": "1.1.5", + "resolved": "https://registry.npmjs.org/jwa/-/jwa-1.1.5.tgz", + "integrity": "sha1-oFUs4CIHQs1S4VN3SjKQXDDnVuU=", + "requires": { + "base64url": "2.0.0", + "buffer-equal-constant-time": "1.0.1", + "ecdsa-sig-formatter": "1.0.9", + "safe-buffer": "5.1.1" + } + }, + "jws": { + "version": "3.1.4", + "resolved": "https://registry.npmjs.org/jws/-/jws-3.1.4.tgz", + "integrity": "sha1-+ei5M46KhHJ31kRLFGT2GIDgUKI=", + "requires": { + "base64url": "2.0.0", + "jwa": "1.1.5", + "safe-buffer": "5.1.1" + } + }, "lodash": { "version": "3.10.1", "resolved": "https://registry.npmjs.org/lodash/-/lodash-3.10.1.tgz", "integrity": "sha1-W/Rejkm6QYnhfUgnid/RW9FAt7Y=", "dev": true }, + "lodash.includes": { + "version": "4.3.0", + "resolved": "https://registry.npmjs.org/lodash.includes/-/lodash.includes-4.3.0.tgz", + "integrity": "sha1-YLuYqHy5I8aMoeUTJUgzFISfVT8=" + }, + "lodash.isboolean": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isboolean/-/lodash.isboolean-3.0.3.tgz", + "integrity": "sha1-bC4XHbKiV82WgC/UOwGyDV9YcPY=" + }, + "lodash.isinteger": { + "version": "4.0.4", + "resolved": "https://registry.npmjs.org/lodash.isinteger/-/lodash.isinteger-4.0.4.tgz", + "integrity": "sha1-YZwK89A/iwTDH1iChAt3sRzWg0M=" + }, + "lodash.isnumber": { + "version": "3.0.3", + "resolved": "https://registry.npmjs.org/lodash.isnumber/-/lodash.isnumber-3.0.3.tgz", + "integrity": "sha1-POdoEMWSjQM1IwGsKHMX8RwLH/w=" + }, + "lodash.isplainobject": { + "version": "4.0.6", + "resolved": "https://registry.npmjs.org/lodash.isplainobject/-/lodash.isplainobject-4.0.6.tgz", + "integrity": "sha1-fFJqUtibRcRcxpC4gWO+BJf1UMs=" + }, + "lodash.isstring": { + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/lodash.isstring/-/lodash.isstring-4.0.1.tgz", + "integrity": "sha1-1SfftUVuynzJu5XV2ur4i6VKVFE=" + }, + "lodash.once": { + "version": "4.1.1", + "resolved": "https://registry.npmjs.org/lodash.once/-/lodash.once-4.1.1.tgz", + "integrity": "sha1-DdOXEhPHxW34gJd9UEyI+0cal6w=" + }, "minimatch": { "version": "3.0.4", "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", @@ -706,6 +798,11 @@ "integrity": "sha1-o1AIsg9BOD7sH7kU9M1d95omQoQ=", "dev": true }, + "ms": { + "version": "2.1.1", + "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.1.tgz", + "integrity": "sha512-tgp+dl5cGk28utYktBsrFqA7HKgrhgPsg6Z/EfhWI4gl1Hwq8B/GmY/0oXZ6nF8hDVesS/FpnYaD/kOWhYQvyg==" + }, "nan": { "version": "2.10.0", "resolved": "https://registry.npmjs.org/nan/-/nan-2.10.0.tgz", @@ -905,8 +1002,7 @@ "safe-buffer": { "version": "5.1.1", "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.1.1.tgz", - "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==", - "dev": true + "integrity": "sha512-kKvNJn6Mm93gAczWVJg7wH+wGYWNrDHdWvpUmHyEsgCtIwwo3bqPtV4tR5tuPaUhTOo/kvhVwd8XwwOllGYkbg==" }, "semver": { "version": "4.3.2", diff --git a/package.json b/package.json index a612130..afadbd1 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "dependencies": { "bcrypt": "^2.0.1", "env2": "^2.2.0", + "jsonwebtoken": "^8.2.1", "pg": "^7.4.1" }, "devDependencies": { diff --git a/src/database/queries/check.js b/src/database/queries/check.js index e8a9460..73ce84b 100644 --- a/src/database/queries/check.js +++ b/src/database/queries/check.js @@ -2,7 +2,7 @@ const dbConnection = require('../db_connection'); const bcrypt = require('bcrypt'); -const getUserData = (email,password,cb)=>{ +const getUserData = (email,password1,cb)=>{ const sql = { text: 'SELECT name,id,role,password FROM users WHERE email = $1', values:[email] @@ -11,12 +11,12 @@ const sql = { if (err) return cb(err) console.log(res.rows[0].password,'ramy'); let hashPassword=res.rows[0].password - bcrypt.compare(password, hashPassword, function(error, result) { + bcrypt.compare(password1, hashPassword, function(error, result) { if (error) { return cb({error,type:'database error'}); } else if(result==true){ - return cb(null,res.rows) + return cb(null,res.rows[0]) } else { return cb({error,type:'password not match'}); diff --git a/src/handler.js b/src/handler.js index 56293d3..a83bf9a 100644 --- a/src/handler.js +++ b/src/handler.js @@ -7,6 +7,7 @@ const check = require('./database/queries/checksignup'); const getUserData = require('./database/queries/check') const hashPassword = require('./hash'); const signupToDb = require('./database/queries/signup'); +const jwt = require('jsonwebtoken'); const contentType = { html:'text/html', css: 'text/css', @@ -172,9 +173,17 @@ const getUserDataFromDB = (request,response)=>{ response.end('

Sorry, user not found

'); } else { - console.log(res); - response.writeHead(302, {"location": "/"}); - response.end('Done') + // console.log(res); + + const userData={userName:res.name,id:res.id,role:res.role} + jwt.sign(JSON.stringify(userData),process.env.JWT_KEY,(err,token)=>{ + response.writeHead(302,{'set-cookie':[`name=${res.name}`, + `token=${token}`], + 'location':'/' + }) + response.end() + }) + // response.writeHead(302, {"location": "/"}); } }); }else{ diff --git a/src/hash.js b/src/hash.js index b32c890..b75ac38 100644 --- a/src/hash.js +++ b/src/hash.js @@ -8,7 +8,6 @@ const hashPassword = (password,cb) => { console.log(hash); cb(null,hash) } - }); }; module.exports=hashPassword