From 18bebe9f9c080d65b6f94cb15fd401ee8e3cd7df Mon Sep 17 00:00:00 2001 From: marwa Date: Wed, 25 Apr 2018 19:07:46 +0300 Subject: [PATCH] checktoken --- package-lock.json | 5 +++++ package.json | 1 + src/handler.js | 16 ++++++++++++++++ src/router.js | 7 ++++--- 4 files changed, 26 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3440e1f..7bb714f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -488,6 +488,11 @@ "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", "dev": true }, + "cookie": { + "version": "0.3.1", + "resolved": "https://registry.npmjs.org/cookie/-/cookie-0.3.1.tgz", + "integrity": "sha1-5+Ch+e9DtMi6klxcWpboBtFoc7s=" + }, "core-util-is": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/core-util-is/-/core-util-is-1.0.2.tgz", diff --git a/package.json b/package.json index afadbd1..924ac1d 100644 --- a/package.json +++ b/package.json @@ -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" diff --git a/src/handler.js b/src/handler.js index a83bf9a..276e708 100644 --- a/src/handler.js +++ b/src/handler.js @@ -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', @@ -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}`], @@ -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)=>{ diff --git a/src/router.js b/src/router.js index 2bee8e6..5bcd23a 100644 --- a/src/router.js +++ b/src/router.js @@ -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) => { @@ -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)