Skip to content

Commit

Permalink
Merge pull request #61 from FACG4/cookies
Browse files Browse the repository at this point in the history
add sending JWT
  • Loading branch information
marwajomaa authored Apr 25, 2018
2 parents ad8685a + deb70de commit 29f443f
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 9 deletions.
100 changes: 98 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"dependencies": {
"bcrypt": "^2.0.1",
"env2": "^2.2.0",
"jsonwebtoken": "^8.2.1",
"pg": "^7.4.1"
},
"devDependencies": {
Expand Down
6 changes: 3 additions & 3 deletions src/database/queries/check.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand All @@ -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'});
Expand Down
15 changes: 12 additions & 3 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down Expand Up @@ -172,9 +173,17 @@ const getUserDataFromDB = (request,response)=>{
response.end('<h1>Sorry, user not found</h1>');
}
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{
Expand Down
1 change: 0 additions & 1 deletion src/hash.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ const hashPassword = (password,cb) => {
console.log(hash);
cb(null,hash)
}

});
};
module.exports=hashPassword

0 comments on commit 29f443f

Please sign in to comment.