From 5dc23d15403914086d180728843e56055ca1917c Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Fri, 10 Feb 2017 11:19:53 +0530 Subject: [PATCH 01/33] [imad-console] Updates ui/index.html --- ui/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/index.html b/ui/index.html index 5a28644db4..a1823d89f8 100644 --- a/ui/index.html +++ b/ui/index.html @@ -9,7 +9,7 @@
- Hi! I am your webapp. + Hi! this is jinesh.
From 773c59dfad1024dde0da2d8e4afd928bb1330181 Mon Sep 17 00:00:00 2001 From: Jinesh Date: Fri, 10 Feb 2017 12:50:29 +0530 Subject: [PATCH 02/33] Made some changes locally --- ui/index.html | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ui/index.html b/ui/index.html index a1823d89f8..fb243c6c59 100644 --- a/ui/index.html +++ b/ui/index.html @@ -10,6 +10,11 @@
Hi! this is jinesh. + whats up guys + + hii + i am learning +
From 5a20d0a8ad4aa35dfde979b0f6d11c0abdc51246 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 11:56:17 +0530 Subject: [PATCH 03/33] [imad-console] Updates server.js --- server.js | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 3d5cece3ab..2b161a826f 100644 --- a/server.js +++ b/server.js @@ -8,7 +8,15 @@ app.use(morgan('combined')); app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); - +app.get('/article-one',function(req,res) { + res.send('Article one is requested and will be served here '); +}); +app.get('/article-two',function(req,res) { + res.send('Article two is requested and will be served here '); +}); +app.get('/article-three',function(req,res) { + res.send('Article three is requested and will be served here '); +}); app.get('/ui/style.css', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'style.css')); }); From c701a94f11179e4ea8903e42afcb46c0b8129dd7 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:05:59 +0530 Subject: [PATCH 04/33] Create article-one.html --- ui/article-one.html | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 ui/article-one.html diff --git a/ui/article-one.html b/ui/article-one.html new file mode 100644 index 0000000000..7be3ff3323 --- /dev/null +++ b/ui/article-one.html @@ -0,0 +1,3 @@ + +

hii

+ From 3a19c85fd17242fe3724ee9e36f987e1ca44202d Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:11:15 +0530 Subject: [PATCH 05/33] [imad-console] Updates ui/article-one.html --- ui/article-one.html | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/ui/article-one.html b/ui/article-one.html index 7be3ff3323..0b32976e93 100644 --- a/ui/article-one.html +++ b/ui/article-one.html @@ -1,3 +1,13 @@ + + + Jinesh + + +

hii

+
+ Home +
+ From f2c0e95d8b680c772c70e1c03504b51a0b521563 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:11:15 +0530 Subject: [PATCH 06/33] [imad-console] Updates server.js --- server.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server.js b/server.js index 2b161a826f..d421e8c307 100644 --- a/server.js +++ b/server.js @@ -9,7 +9,7 @@ app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); app.get('/article-one',function(req,res) { - res.send('Article one is requested and will be served here '); + res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); }); app.get('/article-two',function(req,res) { res.send('Article two is requested and will be served here '); From 2078006cb7c978d941f3e69331474a09ea07cd79 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:15:22 +0530 Subject: [PATCH 07/33] [imad-console] Updates ui/article-one.html --- ui/article-one.html | 1 + 1 file changed, 1 insertion(+) diff --git a/ui/article-one.html b/ui/article-one.html index 0b32976e93..ee3b99e3cd 100644 --- a/ui/article-one.html +++ b/ui/article-one.html @@ -3,6 +3,7 @@ Jinesh +

hii

From e32301394ae0b2e0d918c61b2f81129b681492e0 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:16:43 +0530 Subject: [PATCH 08/33] Create article-two.html --- ui/article-two.html | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ui/article-two.html diff --git a/ui/article-two.html b/ui/article-two.html new file mode 100644 index 0000000000..6cf8e02fe7 --- /dev/null +++ b/ui/article-two.html @@ -0,0 +1,14 @@ + + + + Jinesh1 + + + + +

hii

+
+ Home +
+ + From c146d48351cf6808d7a7be26a79e7bd147ecb198 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:17:16 +0530 Subject: [PATCH 09/33] Create article-three.html --- ui/article-three.html | 14 ++++++++++++++ 1 file changed, 14 insertions(+) create mode 100644 ui/article-three.html diff --git a/ui/article-three.html b/ui/article-three.html new file mode 100644 index 0000000000..ee3b99e3cd --- /dev/null +++ b/ui/article-three.html @@ -0,0 +1,14 @@ + + + + Jinesh + + + + +

hii

+
+ Home +
+ + From 338f4418e00ac2136d79ddfdb98a44902f993874 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:18:45 +0530 Subject: [PATCH 10/33] [imad-console] Updates server.js --- server.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/server.js b/server.js index d421e8c307..17ffdac10e 100644 --- a/server.js +++ b/server.js @@ -12,10 +12,10 @@ app.get('/article-one',function(req,res) { res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); }); app.get('/article-two',function(req,res) { - res.send('Article two is requested and will be served here '); + res.sendFile(path.join(__dirname, 'ui', 'article-two.html')); }); app.get('/article-three',function(req,res) { - res.send('Article three is requested and will be served here '); + res.sendFile(path.join(__dirname, 'ui', 'article-three.html')); }); app.get('/ui/style.css', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'style.css')); From cf85e2879b4b9aeeab1ad2a2b47befa202df0a3c Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:20:39 +0530 Subject: [PATCH 11/33] [imad-console] Updates ui/article-one.html --- ui/article-one.html | 1 + 1 file changed, 1 insertion(+) diff --git a/ui/article-one.html b/ui/article-one.html index ee3b99e3cd..836dcb76b3 100644 --- a/ui/article-one.html +++ b/ui/article-one.html @@ -10,5 +10,6 @@

hii

+

hii this me

From 7535975a0f9d465e2be207c24c917e6259d4211c Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:24:46 +0530 Subject: [PATCH 12/33] [imad-console] Updates ui/article-one.html --- ui/article-one.html | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/ui/article-one.html b/ui/article-one.html index 836dcb76b3..cf832b003e 100644 --- a/ui/article-one.html +++ b/ui/article-one.html @@ -4,12 +4,21 @@ Jinesh + + +

hii

hii this me

+
From f6e1195f9a5fe25b0d46c0c7bc0cce389302a819 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:26:41 +0530 Subject: [PATCH 13/33] [imad-console] Updates ui/article-one.html --- ui/article-one.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/article-one.html b/ui/article-one.html index cf832b003e..aac57ee865 100644 --- a/ui/article-one.html +++ b/ui/article-one.html @@ -8,7 +8,7 @@ From 5badefc5af71a8bb3d3f3a449e520b91d0568749 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:30:39 +0530 Subject: [PATCH 14/33] [imad-console] Updates ui/article-one.html --- ui/article-one.html | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/ui/article-one.html b/ui/article-one.html index aac57ee865..ff52128f09 100644 --- a/ui/article-one.html +++ b/ui/article-one.html @@ -4,13 +4,8 @@ Jinesh - - + +
From 1d02fc114e361e354c191d81d59a2d69aaaaf75c Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 12:30:40 +0530 Subject: [PATCH 15/33] [imad-console] Updates ui/style.css --- ui/style.css | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ui/style.css b/ui/style.css index 71a5f6acc1..eaf8f4f943 100644 --- a/ui/style.css +++ b/ui/style.css @@ -19,4 +19,8 @@ body { .img-medium { height: 200px; } +.st{ + background-color:red; + color:yellow; + } From 84ec08e4bb52f2d9993498d975f998c4361fb702 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 13:00:53 +0530 Subject: [PATCH 16/33] [imad-console] Updates server.js --- server.js | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 17ffdac10e..0826323db7 100644 --- a/server.js +++ b/server.js @@ -5,11 +5,44 @@ var path = require('path'); var app = express(); app.use(morgan('combined')); +var content={ + title:'Jinesh', + heading:'hii', + +}; +function createTemplate (data){ + var title=data.title; + var heading= data.heading; + +var htmlTemplate = ' + + + ${title} + + + + + + +
+

${heading}

+
+ Home +
+

hii this me

+
+ + + +' ; + +return htmlTemplate; +} app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); app.get('/article-one',function(req,res) { - res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); + res.send(createTemplate(article-one)); }); app.get('/article-two',function(req,res) { res.sendFile(path.join(__dirname, 'ui', 'article-two.html')); From 9e8a0d0c551e6165246265c559dd00fbc66d0e73 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 13:49:55 +0530 Subject: [PATCH 17/33] [imad-console] Updates server.js --- server.js | 34 +--------------------------------- 1 file changed, 1 insertion(+), 33 deletions(-) diff --git a/server.js b/server.js index 0826323db7..2e66d125ce 100644 --- a/server.js +++ b/server.js @@ -5,44 +5,12 @@ var path = require('path'); var app = express(); app.use(morgan('combined')); -var content={ - title:'Jinesh', - heading:'hii', - -}; -function createTemplate (data){ - var title=data.title; - var heading= data.heading; -var htmlTemplate = ' - - - ${title} - - - - - - -
-

${heading}

-
- Home -
-

hii this me

-
- - - -' ; - -return htmlTemplate; -} app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); app.get('/article-one',function(req,res) { - res.send(createTemplate(article-one)); + res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); }); app.get('/article-two',function(req,res) { res.sendFile(path.join(__dirname, 'ui', 'article-two.html')); From ff72aec7eede8ff30866b581cac3a2dfe15ad539 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 13:57:59 +0530 Subject: [PATCH 18/33] [imad-console] Updates ui/index.html --- ui/index.html | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ui/index.html b/ui/index.html index fb243c6c59..42e0f7a574 100644 --- a/ui/index.html +++ b/ui/index.html @@ -16,6 +16,9 @@ i am learning
+ From 6f4ed697a97ed59c24ea4e9ac5014b98929cb064 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:11:23 +0530 Subject: [PATCH 19/33] [imad-console] Updates ui/index.html --- ui/index.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/index.html b/ui/index.html index 42e0f7a574..76e1b6c219 100644 --- a/ui/index.html +++ b/ui/index.html @@ -5,7 +5,7 @@
- +

From e1c2fbfbfad749c801eefa006eb85b63a3e380d6 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:25:12 +0530 Subject: [PATCH 20/33] [imad-console] Updates ui/index.html --- ui/index.html | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/ui/index.html b/ui/index.html index 76e1b6c219..a95ad2c41b 100644 --- a/ui/index.html +++ b/ui/index.html @@ -6,6 +6,7 @@
+

@@ -16,9 +17,7 @@ i am learning
- + From 8e4586f147d5b831a91a96981feda3d0a02becf4 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:25:13 +0530 Subject: [PATCH 21/33] [imad-console] Updates server.js --- server.js | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 2e66d125ce..a0971f2702 100644 --- a/server.js +++ b/server.js @@ -5,10 +5,15 @@ var path = require('path'); var app = express(); app.use(morgan('combined')); - app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); +var counter=0; +app.get('/counter', function (req, res) { + counter=counter+1; + + res.send(counter.toString()); +}); app.get('/article-one',function(req,res) { res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); }); From f5dcd3c633c3a1cbd7d8613d3edae24d43c50d63 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:32:35 +0530 Subject: [PATCH 22/33] [imad-console] Updates ui/index.html --- ui/index.html | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/ui/index.html b/ui/index.html index a95ad2c41b..2f1e445138 100644 --- a/ui/index.html +++ b/ui/index.html @@ -17,7 +17,11 @@ i am learning
- + From 152e43a20d127a207d2865687914acd6c681d257 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:38:31 +0530 Subject: [PATCH 23/33] [imad-console] Updates ui/main.js --- ui/main.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/ui/main.js b/ui/main.js index 4cf7ed58de..3fb8568896 100644 --- a/ui/main.js +++ b/ui/main.js @@ -1 +1,9 @@ console.log('Loaded!'); +var button=document.getElementById('counter'); +var counter=0; + +button.onclick=function() { + counter=counter +1; + var span=document.getElementById('count'); + span.innerHTML=counter.toString(); +}; \ No newline at end of file From a9bf3e3b4eabf13daf9b9b3ce203d8ce42e40da1 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:45:26 +0530 Subject: [PATCH 24/33] [imad-console] Updates ui/main.js --- ui/main.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ui/main.js b/ui/main.js index 3fb8568896..6fcfc511a2 100644 --- a/ui/main.js +++ b/ui/main.js @@ -1,4 +1,4 @@ -console.log('Loaded!'); + var button=document.getElementById('counter'); var counter=0; From bfa9c7aa8bffed4aba058dd7129fdf3e8d26a4f4 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:45:31 +0530 Subject: [PATCH 25/33] [imad-console] Updates ui/index.html --- ui/index.html | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/ui/index.html b/ui/index.html index 2f1e445138..319fec2d5c 100644 --- a/ui/index.html +++ b/ui/index.html @@ -18,9 +18,7 @@ From 2dd44d715fef7deb4e38486fc25faf8797235908 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Sat, 11 Feb 2017 14:58:30 +0530 Subject: [PATCH 26/33] [imad-console] Updates ui/main.js --- ui/main.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/ui/main.js b/ui/main.js index 6fcfc511a2..23a9ed76cc 100644 --- a/ui/main.js +++ b/ui/main.js @@ -3,7 +3,20 @@ var button=document.getElementById('counter'); var counter=0; button.onclick=function() { - counter=counter +1; - var span=document.getElementById('count'); - span.innerHTML=counter.toString(); -}; \ No newline at end of file + var request=new XMLHttpRequest(); + request.onreadytstatechange=function() { + if(request.readyState==XMLHttpRequest.Done) { + if(request.status==2000) { + var counter=request.responseText; + var span=document.getElementById('count'); + span.innerHTML=counter.toString(); + } + } + + + +}; +request.open('GET',"http://jinesh220.imad.hasura-app.io/counter",true); +request.send(null); + +}; From 42157cb26010f77ad869ee0db3fb887dfd95e330 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 14 Jun 2017 12:04:42 +0530 Subject: [PATCH 27/33] [imad-console] Updates server.js --- server.js | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/server.js b/server.js index a0971f2702..1dac687f7f 100644 --- a/server.js +++ b/server.js @@ -14,6 +14,16 @@ app.get('/counter', function (req, res) { res.send(counter.toString()); }); +app.get('/test-db',function(req,res) +{ + // make a request + +}); + + + + + app.get('/article-one',function(req,res) { res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); }); From 76443afab013d1b572c01b5f4b3db0c4dbf61aef Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 14 Jun 2017 12:06:58 +0530 Subject: [PATCH 28/33] [imad-console] Updates server.js --- server.js | 5 ----- 1 file changed, 5 deletions(-) diff --git a/server.js b/server.js index 1dac687f7f..8b8771b79e 100644 --- a/server.js +++ b/server.js @@ -14,11 +14,6 @@ app.get('/counter', function (req, res) { res.send(counter.toString()); }); -app.get('/test-db',function(req,res) -{ - // make a request - -}); From 810f22640ff8e88a3d998d21bfab0af775d8f696 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 5 Jul 2017 23:02:40 +0530 Subject: [PATCH 29/33] [imad-console] Updates server.js --- server.js | 247 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 246 insertions(+), 1 deletion(-) diff --git a/server.js b/server.js index 8b8771b79e..0219f77c71 100644 --- a/server.js +++ b/server.js @@ -1,13 +1,258 @@ var express = require('express'); var morgan = require('morgan'); var path = require('path'); +var Pool=require('pg').Pool; +var crypto=require('crypto'); +var bodyParser = require('bodyparser'); +var session = require('express-session'); + + + +var config = { + user: 'jinesh220', + database: 'jinesh220', + host: 'db.imad.hasura-app.io', + port: '5432', + password: db-jinesh220-47751 +}; + + var app = express(); app.use(morgan('combined')); +app.use(bodyParser.json()); +app.use(session({ + secret: 'someRandomSecretValue', + cookie: { maxAge: 1000 * 60 * 60 * 24 * 30} +})); + +function createTemplate (data) { + var title = data.title; + var date = data.date; + var heading = data.heading; + var content = data.content; + + var htmlTemplate = ` + + + + ${title} + + + + + +
+
+ Home +
+
+

+ ${heading} +

+
+ ${date.toDateString()} +
+
+ ${content} +
+
+

Comments

+
+
+
+
Loading comments...
+
+
+ + + + `; + return htmlTemplate; +} + + app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); + + +function hash (input, salt) { + // How do we create a hash? + var hashed = crypto.pbkdf2Sync(input, salt, 10000, 512, 'sha512'); + return ["pbkdf2", "10000", salt, hashed.toString('hex')].join('$'); +} + +app.get('/hash/:input', function(req, res) { + var hashedString = hash(req.params.input, 'this-is-some-random-string'); + res.send(hashedString); +}); + +app.post('/create-user', function (req, res) { + // username, password + // {"username": "tanmai", "password": "password"} + // JSON + var username = req.body.username; + var password = req.body.password; + var salt = crypto.randomBytes(128).toString('hex'); + var dbString = hash(password, salt); + pool.query('INSERT INTO "user" (username, password) VALUES ($1, $2)', [username, dbString], function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + res.send('User successfully created: ' + username); + } + }); +}); + +app.post('/login', function (req, res) { + var username = req.body.username; + var password = req.body.password; + + pool.query('SELECT * FROM "user" WHERE username = $1', [username], function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + if (result.rows.length === 0) { + res.status(403).send('username/password is invalid'); + } else { + // Match the password + var dbString = result.rows[0].password; + var salt = dbString.split('$')[2]; + var hashedPassword = hash(password, salt); // Creating a hash based on the password submitted and the original salt + if (hashedPassword === dbString) { + + // Set the session + req.session.auth = {userId: result.rows[0].id}; + // set cookie with a session id + // internally, on the server side, it maps the session id to an object + // { auth: {userId }} + + res.send('credentials correct!'); + + } else { + res.status(403).send('username/password is invalid'); + } + } + } + }); +}); + +app.get('/check-login', function (req, res) { + if (req.session && req.session.auth && req.session.auth.userId) { + // Load the user object + pool.query('SELECT * FROM "user" WHERE id = $1', [req.session.auth.userId], function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + res.send(result.rows[0].username); + } + }); + } else { + res.status(400).send('You are not logged in'); + } +}); + +app.get('/logout', function (req, res) { + delete req.session.auth; + res.send('Logged out!

Back to home'); +}); + +var pool = new Pool(config); + +app.get('/get-articles', function (req, res) { + // make a select request + // return a response with the results + pool.query('SELECT * FROM article ORDER BY date DESC', function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + res.send(JSON.stringify(result.rows)); + } + }); +}); + +app.get('/get-comments/:articleName', function (req, res) { + // make a select request + // return a response with the results + pool.query('SELECT comment.*, "user".username FROM article, comment, "user" WHERE article.title = $1 AND article.id = comment.article_id AND comment.user_id = "user".id ORDER BY comment.timestamp DESC', [req.params.articleName], function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + res.send(JSON.stringify(result.rows)); + } + }); +}); + +app.post('/submit-comment/:articleName', function (req, res) { + // Check if the user is logged in + if (req.session && req.session.auth && req.session.auth.userId) { + // First check if the article exists and get the article-id + pool.query('SELECT * from article where title = $1', [req.params.articleName], function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + if (result.rows.length === 0) { + res.status(400).send('Article not found'); + } else { + var articleId = result.rows[0].id; + // Now insert the right comment for this article + pool.query( + "INSERT INTO comment (comment, article_id, user_id) VALUES ($1, $2, $3)", + [req.body.comment, articleId, req.session.auth.userId], + function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + res.status(200).send('Comment inserted!') + } + }); + } + } + }); + } else { + res.status(403).send('Only logged in users can comment'); + } +}); + +app.get('/articles/:articleName', function (req, res) { + // SELECT * FROM article WHERE title = '\'; DELETE WHERE a = \'asdf' + pool.query("SELECT * FROM article WHERE title = $1", [req.params.articleName], function (err, result) { + if (err) { + res.status(500).send(err.toString()); + } else { + if (result.rows.length === 0) { + res.status(404).send('Article not found'); + } else { + var articleData = result.rows[0]; + res.send(createTemplate(articleData)); + } + } + }); +}); + +app.get('/ui/:fileName', function (req, res) { + res.sendFile(path.join(__dirname, 'ui', req.params.fileName)); +}); + + + +var port = 8080; // Use 8080 for local development because you might already have apache running on 80 +app.listen(8080, function () { + console.log(`IMAD course app listening on port ${port}!`); +}); + + + + + + + + + + +/* var counter=0; app.get('/counter', function (req, res) { counter=counter+1; @@ -40,4 +285,4 @@ app.get('/ui/madi.png', function (req, res) { var port = 8080; // Use 8080 for local development because you might already have apache running on 80 app.listen(8080, function () { console.log(`IMAD course app listening on port ${port}!`); -}); +});*/ From c88cb42151917bacfbf1f61473ea293c91cff372 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 5 Jul 2017 23:05:15 +0530 Subject: [PATCH 30/33] [imad-console] Updates server.js --- server.js | 112 ++++-------------------------------------------------- 1 file changed, 7 insertions(+), 105 deletions(-) diff --git a/server.js b/server.js index 0219f77c71..f1a74feab4 100644 --- a/server.js +++ b/server.js @@ -1,23 +1,19 @@ var express = require('express'); var morgan = require('morgan'); var path = require('path'); -var Pool=require('pg').Pool; -var crypto=require('crypto'); -var bodyParser = require('bodyparser'); +var Pool = require('pg').Pool; +var crypto = require('crypto'); +var bodyParser = require('body-parser'); var session = require('express-session'); - - var config = { user: 'jinesh220', database: 'jinesh220', host: 'db.imad.hasura-app.io', port: '5432', - password: db-jinesh220-47751 + password:db-jinesh220-47751 }; - - var app = express(); app.use(morgan('combined')); app.use(bodyParser.json()); @@ -26,6 +22,7 @@ app.use(session({ cookie: { maxAge: 1000 * 60 * 60 * 24 * 30} })); + function createTemplate (data) { var title = data.title; var date = data.date; @@ -71,8 +68,6 @@ function createTemplate (data) { return htmlTemplate; } - - app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); @@ -84,6 +79,7 @@ function hash (input, salt) { return ["pbkdf2", "10000", salt, hashed.toString('hex')].join('$'); } + app.get('/hash/:input', function(req, res) { var hashedString = hash(req.params.input, 'this-is-some-random-string'); res.send(hashedString); @@ -106,58 +102,9 @@ app.post('/create-user', function (req, res) { }); }); -app.post('/login', function (req, res) { - var username = req.body.username; - var password = req.body.password; - - pool.query('SELECT * FROM "user" WHERE username = $1', [username], function (err, result) { - if (err) { - res.status(500).send(err.toString()); - } else { - if (result.rows.length === 0) { - res.status(403).send('username/password is invalid'); - } else { - // Match the password - var dbString = result.rows[0].password; - var salt = dbString.split('$')[2]; - var hashedPassword = hash(password, salt); // Creating a hash based on the password submitted and the original salt - if (hashedPassword === dbString) { - - // Set the session - req.session.auth = {userId: result.rows[0].id}; - // set cookie with a session id - // internally, on the server side, it maps the session id to an object - // { auth: {userId }} - - res.send('credentials correct!'); - - } else { - res.status(403).send('username/password is invalid'); - } - } - } - }); -}); -app.get('/check-login', function (req, res) { - if (req.session && req.session.auth && req.session.auth.userId) { - // Load the user object - pool.query('SELECT * FROM "user" WHERE id = $1', [req.session.auth.userId], function (err, result) { - if (err) { - res.status(500).send(err.toString()); - } else { - res.send(result.rows[0].username); - } - }); - } else { - res.status(400).send('You are not logged in'); - } -}); -app.get('/logout', function (req, res) { - delete req.session.auth; - res.send('Logged out!

Back to home'); -}); + var pool = new Pool(config); @@ -237,52 +184,7 @@ app.get('/ui/:fileName', function (req, res) { }); - var port = 8080; // Use 8080 for local development because you might already have apache running on 80 app.listen(8080, function () { console.log(`IMAD course app listening on port ${port}!`); }); - - - - - - - - - - -/* -var counter=0; -app.get('/counter', function (req, res) { - counter=counter+1; - - res.send(counter.toString()); -}); - - - - - -app.get('/article-one',function(req,res) { - res.sendFile(path.join(__dirname, 'ui', 'article-one.html')); -}); -app.get('/article-two',function(req,res) { - res.sendFile(path.join(__dirname, 'ui', 'article-two.html')); -}); -app.get('/article-three',function(req,res) { - res.sendFile(path.join(__dirname, 'ui', 'article-three.html')); -}); -app.get('/ui/style.css', function (req, res) { - res.sendFile(path.join(__dirname, 'ui', 'style.css')); -}); - -app.get('/ui/madi.png', function (req, res) { - res.sendFile(path.join(__dirname, 'ui', 'madi.png')); -}); - - -var port = 8080; // Use 8080 for local development because you might already have apache running on 80 -app.listen(8080, function () { - console.log(`IMAD course app listening on port ${port}!`); -});*/ From a5e7998dc1cc7c9f7a4289d94f1ce98fc1fa24a2 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 5 Jul 2017 23:11:46 +0530 Subject: [PATCH 31/33] [imad-console] Updates ui/index.html --- ui/index.html | 35 ++++++++++++++++++++--------------- 1 file changed, 20 insertions(+), 15 deletions(-) diff --git a/ui/index.html b/ui/index.html index 319fec2d5c..66b071da62 100644 --- a/ui/index.html +++ b/ui/index.html @@ -4,22 +4,27 @@ -
- -
+
+
+ +
+

About Me

+
+ Hi. My name is Tanmai Gopal
+ I work at Hasura +
+
+
+
Loading login status...
+
+
+

My Articles

+
+
Loading articles...
+
+
-
-
- Hi! this is jinesh. - whats up guys - - hii - i am learning - -
- From 750b58e8f9e8ea00c359b5fd13cff21f87b12965 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 5 Jul 2017 23:12:05 +0530 Subject: [PATCH 32/33] [imad-console] Updates ui/main.js --- ui/main.js | 149 +++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 133 insertions(+), 16 deletions(-) diff --git a/ui/main.js b/ui/main.js index 23a9ed76cc..57a5718493 100644 --- a/ui/main.js +++ b/ui/main.js @@ -1,22 +1,139 @@ -var button=document.getElementById('counter'); -var counter=0; +function loadLoginForm () { + var loginHtml = ` +

Login/Register to unlock awesome features

+ + +

+ + + `; + document.getElementById('login_area').innerHTML = loginHtml; + + // Submit username/password to login + var submit = document.getElementById('login_btn'); + submit.onclick = function () { + // Create a request object + var request = new XMLHttpRequest(); + + // Capture the response and store it in a variable + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + // Take some action + if (request.status === 200) { + submit.value = 'Sucess!'; + } else if (request.status === 403) { + submit.value = 'Invalid credentials. Try again?'; + } else if (request.status === 500) { + alert('Something went wrong on the server'); + submit.value = 'Login'; + } else { + alert('Something went wrong on the server'); + submit.value = 'Login'; + } + loadLogin(); + } + // Not done yet + }; + + // Make the request + var username = document.getElementById('username').value; + var password = document.getElementById('password').value; + console.log(username); + console.log(password); + request.open('POST', '/login', true); + request.setRequestHeader('Content-Type', 'application/json'); + request.send(JSON.stringify({username: username, password: password})); + submit.value = 'Logging in...'; + + }; + + var register = document.getElementById('register_btn'); + register.onclick = function () { + // Create a request object + var request = new XMLHttpRequest(); + + // Capture the response and store it in a variable + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + // Take some action + if (request.status === 200) { + alert('User created successfully'); + register.value = 'Registered!'; + } else { + alert('Could not register the user'); + register.value = 'Register'; + } + } + }; + + // Make the request + var username = document.getElementById('username').value; + var password = document.getElementById('password').value; + console.log(username); + console.log(password); + request.open('POST', '/create-user', true); + request.setRequestHeader('Content-Type', 'application/json'); + request.send(JSON.stringify({username: username, password: password})); + register.value = 'Registering...'; + + }; +} -button.onclick=function() { - var request=new XMLHttpRequest(); - request.onreadytstatechange=function() { - if(request.readyState==XMLHttpRequest.Done) { - if(request.status==2000) { - var counter=request.responseText; - var span=document.getElementById('count'); - span.innerHTML=counter.toString(); +function loadLoggedInUser (username) { + var loginArea = document.getElementById('login_area'); + loginArea.innerHTML = ` +

Hi ${username}

+ Logout + `; +} + +function loadLogin () { + // Check if the user is already logged in + var request = new XMLHttpRequest(); + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + if (request.status === 200) { + loadLoggedInUser(this.responseText); + } else { + loadLoginForm(); } } + }; + + request.open('GET', '/check-login', true); + request.send(null); +} + +function loadArticles () { + // Check if the user is already logged in + var request = new XMLHttpRequest(); + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + var articles = document.getElementById('articles'); + if (request.status === 200) { + var content = '
    '; + var articleData = JSON.parse(this.responseText); + for (var i=0; i< articleData.length; i++) { + content += `
  • + ${articleData[i].heading} + (${articleData[i].date.split('T')[0]})
  • `; + } + content += "
" + articles.innerHTML = content; + } else { + articles.innerHTML('Oops! Could not load all articles!') + } + } + }; + + request.open('GET', '/get-articles', true); + request.send(null); +} + - - -}; -request.open('GET',"http://jinesh220.imad.hasura-app.io/counter",true); -request.send(null); +// The first thing to do is to check if the user is logged in! +loadLogin(); -}; +// Now this is something that we could have directly done on the server-side using templating too! +loadArticles(); \ No newline at end of file From d7511114e20d1e126ff97e5c62aabc62341dc986 Mon Sep 17 00:00:00 2001 From: Jinesh220 Date: Wed, 5 Jul 2017 23:15:55 +0530 Subject: [PATCH 33/33] Create article.js --- ui/article.js | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 ui/article.js diff --git a/ui/article.js b/ui/article.js new file mode 100644 index 0000000000..aab9571dd1 --- /dev/null +++ b/ui/article.js @@ -0,0 +1,100 @@ +// Eg: coco98.imad.hasura-app.io/articles/article-one will result in article-one +var currentArticleTitle = window.location.pathname.split('/')[2]; + +function loadCommentForm () { + var commentFormHtml = ` +
Submit a comment
+ +
+ +
+ `; + document.getElementById('comment_form').innerHTML = commentFormHtml; + + // Submit username/password to login + var submit = document.getElementById('submit'); + submit.onclick = function () { + // Create a request object + var request = new XMLHttpRequest(); + + // Capture the response and store it in a variable + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + // Take some action + if (request.status === 200) { + // clear the form & reload all the comments + document.getElementById('comment_text').value = ''; + loadComments(); + } else { + alert('Error! Could not submit comment'); + } + submit.value = 'Submit'; + } + }; + + // Make the request + var comment = document.getElementById('comment_text').value; + request.open('POST', '/submit-comment/' + currentArticleTitle, true); + request.setRequestHeader('Content-Type', 'application/json'); + request.send(JSON.stringify({comment: comment})); + submit.value = 'Submitting...'; + + }; +} + +function loadLogin () { + // Check if the user is already logged in + var request = new XMLHttpRequest(); + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + if (request.status === 200) { + loadCommentForm(this.responseText); + } + } + }; + + request.open('GET', '/check-login', true); + request.send(null); +} + +function escapeHTML (text) +{ + var $text = document.createTextNode(text); + var $div = document.createElement('div'); + $div.appendChild($text); + return $div.innerHTML; +} + +function loadComments () { + // Check if the user is already logged in + var request = new XMLHttpRequest(); + request.onreadystatechange = function () { + if (request.readyState === XMLHttpRequest.DONE) { + var comments = document.getElementById('comments'); + if (request.status === 200) { + var content = ''; + var commentsData = JSON.parse(this.responseText); + for (var i=0; i< commentsData.length; i++) { + var time = new Date(commentsData[i].timestamp); + content += `
+

${escapeHTML(commentsData[i].comment)}

+
+ ${commentsData[i].username} - ${time.toLocaleTimeString()} on ${time.toLocaleDateString()} +
+
`; + } + comments.innerHTML = content; + } else { + comments.innerHTML('Oops! Could not load comments!'); + } + } + }; + + request.open('GET', '/get-comments/' + currentArticleTitle, true); + request.send(null); +} + + +// The first thing to do is to check if the user is logged in! +loadLogin(); +loadComments();