SHARE
TWEET

Untitled

a guest Nov 6th, 2018 120 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const express = require('express');
  2. const app = express();
  3. const session = require('express-session');
  4. const path = require('path');
  5. const bodyparser = require('body-parser');
  6. const mysql = require('mysql');
  7. const cookieParser = require('cookie-parser');
  8.  
  9.  
  10. //Serve public folders static files
  11. app.use(express.static(path.join(__dirname, 'public')));
  12. app.engine('html', require('ejs').renderFile);
  13. app.set('view engine', 'html');
  14. app.set('views', __dirname + "/public/");
  15.  
  16. //Use bodyparser to parse POST forms (eg. req.body.'input name')
  17. app.use(bodyparser.urlencoded({extended: false}));
  18.  
  19. //Use cookie parser to manage cookies
  20. app.use(cookieParser());
  21.  
  22. //Setup session
  23. app.use(session({secret: 'SepsisMies123'}));
  24.  
  25. var db = mysql.createConnection({
  26.     host: "localhost",
  27.     user: "dbuser",
  28.     password: "Sepsis123Database",
  29.     database: "AjoneuvonSeuranta"
  30. });
  31.  
  32. /*
  33. db.connect(function(err) {
  34.     if (err) throw err;
  35. });*/
  36.  
  37.  
  38. app.get('/', function(req, res) {
  39.     if (req.session.account_id) {
  40.         console.log("Rendering profile because session exists");
  41.         res.redirect('/profile');
  42.     }
  43.     else {
  44.         console.log("rending index no session");
  45.         res.render('home.html');
  46.     }
  47. });
  48.  
  49. app.get('/profile', function(req, res) {
  50.     if (req.session.account_id) {
  51.         console.log("Rendering profile because session exists");
  52.         res.render('profile.html');
  53.     }
  54.     else {
  55.         console.log("cant access profile without session");
  56.         res.redirect('/');
  57.     }
  58. });
  59.  
  60. app.get('/logout', function(req, res) {
  61.     req.session.destroy(function(err) {
  62.         if (err) {
  63.             res.negotiate(err);
  64.         }
  65.     });
  66.     res.redirect('/');
  67.     console.log('Deleted session id!');
  68. });
  69.  
  70. app.post('/validateLogin', function(req, res) {
  71.     console.log("Trying to login with " + req.body.uname + req.body.psw);
  72.     /*db.query("SELECT * FROM account where user_name = '" + req.body.uname + "' AND password = '" + req.body.psw + "';",
  73.         function(err, result, fields) {
  74.             if (err) throw err;
  75.             if (Object.keys(result).length == 1) {
  76.                 console.log("Found user!");
  77.                 req.session.account_id = result.account_id;
  78.                 res.render('profile.html');
  79.             }
  80.             else {
  81.                 console.log("User not found!");
  82.                 res.render('index.html');
  83.             }
  84.     });*/
  85.     if (req.body.uname == "user" && req.body.psw == "pass") {
  86.         req.session.account_id = 1;
  87.         res.render('profile.html');
  88.         console.log("Valid user pass!");
  89.     }
  90.     else {
  91.         res.redirect('/');
  92.         console.log("Invalid user pass!");
  93.     }
  94.  
  95. });
  96.  
  97. const server = app.listen(8080, function() {
  98.     console.log("Server running at port 8080!");
  99. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top