diff --git a/server.js b/server.js index 2e386d0b041..bd567d84cc7 100644 --- a/server.js +++ b/server.js @@ -1,18 +1,248 @@ var express = require('express'); var morgan = require('morgan'); var path = require('path'); +var Pool = require('pg').Pool; +var crypto = require('crypto'); +var bodyParser = require('body-parser'); +var session = require('express-session'); + +var config = { + user: 'ssttrinath', + database: 'ssttrinath', + host: 'db.imad.hasura-app.io', + port: '5432', + password: process.env.DB_PASSWORD +}; var app = express(); app.use(morgan('combined')); +app.use(bodyParser.json()); +app.use(session({ + secret: 'someRandomStringValue', + cookie: {maxAge: 1000* 60* 60} +})); + +var counter=0; +app.get('/counter', function(req, res) { + counter++; + res.send(counter.toString()); +}); + +function hash(input, salt) { + 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 + //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 is successfully created with username '+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 incorrect!!!'); + } else { + //match the password + var dbString = result.rows[0].password; + var salt = dbString.split('$')[2]; + var hashedPassword = hash(password, salt); //creating hashed password with the password submitted by the user while login + if(hashedPassword === dbString) { //matching stored password and subitted password. + + //set a 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 on the object + //{auth: {userId }} + + res.send('credentials are correct!'); + } else { + res.status(403).send('Username/Password is incorrect!!!'); + } + } + } + }); +}); + +app.get('/check-login', function(req, res) { + if(req.session && req.session.auth && req.session.auth.userId) { + res.send('yes' ); + } else { + res.send('no'); + } +}); + +app.get('/logout', function(req, res) { + delete req.session.auth; + res.send('Logged out!'); +}); + +/* +app.get('renderlogin', function(req, res) { + var region = document.getElementById('') +}); + +app.get('renderlogin', function(req, res) { + +}); +*/ app.get('/', function (req, res) { res.sendFile(path.join(__dirname, 'ui', 'index.html')); }); +var names=[]; +app.get('/submit-name', function(req, res) { + var name = req.query.name; + names.push(name); + res.send(JSON.stringify(names)); +}); + +var pool = new Pool(config); +/* +app.get('/test-db', function(req, res) { + //make a select request + //return a response with the results + pool.query('SELECT * FROM test', function(err, result){ + if(err) { + res.status(500).send(err.toString()); + } else { + res.send(JSON.stringify(result.rows)); + } + }); +}); +*/ +var createTemplate = function(data) { + var title = data.title; + var heading = data.heading; + var date = data.date; + var content = data.content; + var template = ` + + +
+
Comments
++
+ + +