From b6ec1d17038eeae5395e6f3daff930c4e7d16e04 Mon Sep 17 00:00:00 2001
From: Amber Elferink <a.s.elferink@students.uu.nl>
Date: Tue, 10 Apr 2018 01:55:06 +0200
Subject: [PATCH] profile 404 errors

---
 app.js                          |  6 +++++-
 public/javascripts/login.js     |  5 +++--
 public/javascripts/navbar.js    |  3 ++-
 public/javascripts/profile.js   |  0
 routes/checkLogin.js            | 26 +++++++++--------------
 routes/profile.js               | 37 +++++++++++++++++++++++++++++++++
 views/layouts/layoutProfile.hbs | 23 ++++++++++++++++++++
 views/profile.hbs               |  1 +
 8 files changed, 81 insertions(+), 20 deletions(-)
 create mode 100644 public/javascripts/profile.js
 create mode 100644 routes/profile.js
 create mode 100644 views/layouts/layoutProfile.hbs
 create mode 100644 views/profile.hbs

diff --git a/app.js b/app.js
index e9146e1..bc1cc96 100644
--- a/app.js
+++ b/app.js
@@ -26,6 +26,8 @@ var loadProducts = require('./routes/loadProducts');
 var checkLoginRouter = require('./routes/checkLogin');
 var registerUserRouter = require('./routes/registerDatabase');
 
+var profileRouter = require('./routes/profile');
+
 var app = express();
 
 // view engine setup
@@ -56,7 +58,9 @@ app.use('/register', registerRouter);
 app.use('/api/addUserName', addUserNameRouter);
 app.use('/api/checkLogin', checkLoginRouter);
 app.use('/api/products', loadProducts);
-app.use('/api/register', registerUserRouter)
+app.use('/api/register', registerUserRouter);
+
+app.use('/profile', profileRouter);
 
 // catch 404 and forward to error handler
 app.use(function(req, res, next) {
diff --git a/public/javascripts/login.js b/public/javascripts/login.js
index 46b6684..f217c0f 100644
--- a/public/javascripts/login.js
+++ b/public/javascripts/login.js
@@ -66,8 +66,9 @@ function postLogin(callback, un, pw) {
 }
 
 function giveAlert(returnValue) {
-    if (returnValue == true) {
-        alert("Login was successful");
+    if (returnValue.boolLoginCorrect == true) {
+        //since username must be unique for the database, it can be used for the url
+        window.location.replace("/profile?" + returnValue.currentuser);
     }
     else
     {
diff --git a/public/javascripts/navbar.js b/public/javascripts/navbar.js
index 20bfc82..71309a1 100644
--- a/public/javascripts/navbar.js
+++ b/public/javascripts/navbar.js
@@ -8,7 +8,8 @@ $('#logout').click(function () {
         .done(function (data) {
             //deze done functie logt het naar de javascript console en print het op de pagina als txt
             //console.log('GET response:', JSON.stringify(data, "", 2));
-            console.log(data);
+            window.location.replace("/");
+            alert(data); //gives "logout was succesful" if user was logged in, otherwise nothing.
         })
         //als het niet goed is gegaan, doet hij de fail hieronder
         .fail(function (jqXHR, textStatus, err) {
diff --git a/public/javascripts/profile.js b/public/javascripts/profile.js
new file mode 100644
index 0000000..e69de29
diff --git a/routes/checkLogin.js b/routes/checkLogin.js
index 4e4073c..7bb6bb4 100644
--- a/routes/checkLogin.js
+++ b/routes/checkLogin.js
@@ -9,6 +9,7 @@ var app = express();
 session = require('express-session');
 
 app.use(session({
+    path: '/profile',
     secret: '2C44-4D44-WppQ38S',
     resave: true,
     saveUninitialized: false,
@@ -18,16 +19,7 @@ app.use(session({
 
 /*If a get or post loads a page, for instance /myprofile, it wil look if the person is authorised to see it using this.
  */
-var auth = function (req, res, next, user) {
-    if(req.session && req.session.user === user)
-    {
-        return next();
-    }
-    else
-    {
-        return res.sendStatus(401);
-    }
-};
+
 
 
 
@@ -38,12 +30,16 @@ router.post('/login', function (req, res) {
     //alles binnen deze functie doet hij pas nadat de callback is uitgevoerd. Als je res.send(data);
     //dus onder de }); zet, dan krijg je undefined terug omdat de callback nog niet klaar was, toen hij was uitgevoerd
     checkLoginWithDatabase(function(err, returnValues, username){
-        console.log(username);
+        //login is correct
         if(returnValues == true)
         {
+            req.session.username
             req.session.user = username;
-            res.send(returnValues);
+            res.send(
+                {"boolLoginCorrect": returnValues, "currentuser": username}
+            );
         }
+        //login is not correct
         else
         {
             res.send(returnValues);
@@ -59,10 +55,8 @@ router.get('/logout', function (req, res) {
     res.send("logout success!");
 });
 
-router.get('/profile', auth, function (req, res) {
-    user = req.query;
-    res.send("You can only see this after you've logged in.")
-});
+
+
 
 module.exports = router;
 
diff --git a/routes/profile.js b/routes/profile.js
new file mode 100644
index 0000000..144cd51
--- /dev/null
+++ b/routes/profile.js
@@ -0,0 +1,37 @@
+
+var sqlite3 = require('sqlite3').verbose();
+var fs = require("fs");
+var file = __dirname + "/../ConnectionJs/fastdining.db";
+var exists = fs.existsSync(file);
+var express = require('express');
+var router = express.Router();
+var app = express();
+session = require('express-session');
+
+router.get('/:username', function(req, res, next) {
+    console.log(req.param('username'));
+    res.render('profile', {title: 'Profile Fast Dining', layout: 'layoutProfile'});
+
+});
+//authorises if the user may access the page
+var auth = function (req, res, next) {
+    if(req.session && req.session.user === req.query)
+    {
+        return next();
+    }
+    else
+    {
+        return res.sendStatus(401); //login session not active/unauthorised
+    }
+};
+
+
+/*
+router.get('/', auth, function (req, res) {
+    console.log(req.query);
+    res.render('shop/products', {title: 'Buy Products', layout: 'productSearchLayout'});
+});*/
+
+
+
+module.exports = router;
diff --git a/views/layouts/layoutProfile.hbs b/views/layouts/layoutProfile.hbs
new file mode 100644
index 0000000..346ef83
--- /dev/null
+++ b/views/layouts/layoutProfile.hbs
@@ -0,0 +1,23 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Fast Dining - {{title}}</title>
+    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
+    <script defer src="https://use.fontawesome.com/releases/v5.0.9/js/all.js" integrity="sha384-8iPTk2s/jMVj81dnzb/iFR2sdA7u06vHJyyLlAd4snFpCl/SnyUjRrbdJsw1pGIl" crossorigin="anonymous"></script>
+
+    <link rel='stylesheet' href='stylesheets/style.css' /><!-- dit zo laten staan, de express-handlebars zoekt hem automatisch in de stylesheets map-->
+
+</head>
+<body>
+
+{{>header}} <!-- dit commando van express-handlebars zoekt by default in partials folder-->
+<main class="content">
+    {{{body}}}
+</main>
+<!-- basis javascript van bootstrap-->
+<script src="libraries/jquery.min.js"></script>
+<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
+<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
+<script type=text/javascript src="javascripts/profile.js"></script>
+</body>
+</html>
\ No newline at end of file
diff --git a/views/profile.hbs b/views/profile.hbs
new file mode 100644
index 0000000..97ca6dd
--- /dev/null
+++ b/views/profile.hbs
@@ -0,0 +1 @@
+<p>Hi there</p>
\ No newline at end of file
-- 
GitLab