Skip to content

Commit

Permalink
Merge pull request #63 from FACG4/token
Browse files Browse the repository at this point in the history
checktoken
  • Loading branch information
RamyAlshurafa authored Apr 25, 2018
2 parents ad89f6d + 18bebe9 commit 3cf9b40
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
5 changes: 5 additions & 0 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 @@ -19,6 +19,7 @@
"homepage": "https://github.com/FACG4/w6_Stop_Go_Cont#readme",
"dependencies": {
"bcrypt": "^2.0.1",
"cookie": "^0.3.1",
"env2": "^2.2.0",
"jsonwebtoken": "^8.2.1",
"pg": "^7.4.1"
Expand Down
16 changes: 16 additions & 0 deletions src/handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const getUserData = require('./database/queries/check')
const hashPassword = require('./hash');
const signupToDb = require('./database/queries/signup');
const jwt = require('jsonwebtoken');
const cookie = require('cookie');
const contentType = {
html:'text/html',
css: 'text/css',
Expand Down Expand Up @@ -176,6 +177,7 @@ const getUserDataFromDB = (request,response)=>{
// console.log(res);

const userData={userName:res.name,id:res.id,role:res.role}
console.log(userData);
jwt.sign(JSON.stringify(userData),process.env.JWT_KEY,(err,token)=>{
response.writeHead(302,{'set-cookie':[`name=${res.name}`,
`token=${token}`],
Expand All @@ -193,7 +195,21 @@ const getUserDataFromDB = (request,response)=>{
});
}

const checkToken = ('/login',response){
if(request.headers.cookie){
const obj = cookie.parse(request.headers.cookie);
if(obj.token ){

// console.log(obj);
response.writeHead(302,{'location':'/'})
response.end()
}
} else{

handler.serveFiles('/login.html', response);
}

}


const logout=(request,response)=>{
Expand Down
7 changes: 4 additions & 3 deletions src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const http = require('http');
const fs = require('fs');
const pg = require('pg');
const handler = require('./handler');
const cookie = require('cookie');
const type = ['/css/style.css','/js/dom.js','/favicon.ico','/js/index.js']

const router = (request, response) => {
Expand All @@ -11,10 +12,10 @@ const router = (request, response) => {
}else if (url === "/create-post" && request.method === 'POST') {
handler.sendDataToDB(request,response)
}else if (url==="/login") {
handler.serveFiles('/login.html', response);
}else if (url==="/signup") {
handler.checkToken(request,response)
}else if (url==="/signup") {
handler.serveFiles('/register.html', response);
}else if (url==="/checkUser"&& request.method === 'POST') {
}else if (url==="/checkUser") {
handler.getUserDataFromDB(request,response)
}else if (url==="/getdata") {
handler.getDBData(response)
Expand Down

0 comments on commit 3cf9b40

Please sign in to comment.