From 1862aa4aac13059eb05795d06273db854c5ca310 Mon Sep 17 00:00:00 2001
From: justin <justin.foltz@etu.hesge.ch>
Date: Tue, 21 Jan 2020 12:11:48 +0100
Subject: [PATCH] Fix bug

---
 projet/DB/DB.js            | 2 +-
 projet/app.js              | 9 +++++----
 projet/public/js/map.js    | 5 ++++-
 projet/views/login.html    | 4 ++--
 projet/views/register.html | 6 +++---
 5 files changed, 15 insertions(+), 11 deletions(-)

diff --git a/projet/DB/DB.js b/projet/DB/DB.js
index 5237f05..94146d3 100644
--- a/projet/DB/DB.js
+++ b/projet/DB/DB.js
@@ -156,7 +156,7 @@ checkToken = async (id, token) => {
   const containsId = await containId(id);
   if(containsId){
     const user = await Profil.findOne({ "_id" : id }).select('+token');
-    if(user.toObject().token === token)
+    if(user.toObject().token !== null && user.toObject().token === token)
       return true; 
   }
   return false; 
diff --git a/projet/app.js b/projet/app.js
index 8f4a6d3..074741a 100644
--- a/projet/app.js
+++ b/projet/app.js
@@ -11,15 +11,16 @@ app.use(bodyParser.urlencoded({ extended: true }));
 require('dotenv').config();
 const db = require("./DB/DB.js");
 
-db.registration("Aze", "aze", "aze");
+// Création d'un user pour la démo
+db.registration("demo", "demo", "Demo");
 
 // Make sure that the user token is valid
-const auth = async(req, res, next) => {
+let auth = async(req, res, next) => {
     try {
-
         const token = req.header('Authorization').replace('Bearer ', '');
         const data = jwt.verify(token, process.env.JWT_SECRET);
-        if(!db.checkToken(data.userId, token)) {
+        let checkToken = await db.checkToken(data.userId, token);
+        if(!checkToken) {
             throw new Error();
         }
         req.userId = data.userId;
diff --git a/projet/public/js/map.js b/projet/public/js/map.js
index 028304f..096b60f 100644
--- a/projet/public/js/map.js
+++ b/projet/public/js/map.js
@@ -90,6 +90,7 @@ function getFavoriteEvents() {
     },
     error: (resultat, statut, erreur) => {
       if( erreur === "Unauthorized") {
+        alert("getFavoriteEvents");
         window.location = "/login";
       }
     }
@@ -169,6 +170,7 @@ function getEvents() {
     url: URL,
     headers: jsonHeader(),
     success: (data) => {
+      console.log("DATA get event", data);
       storeToken(data.token);
       if(data.events) {
         data.events.forEach( event => {
@@ -180,6 +182,7 @@ function getEvents() {
     },
     error: (resultat, statut, erreur) => {
       if( erreur === "Unauthorized") {
+        alert("getEvents");
         window.location = "/login";
       }
     }
@@ -305,7 +308,7 @@ $.ajax({
   headers: jsonHeader(),
   success: (data) => {
       storeToken(data.token);
-      $("#welcomeText").html("Hey <B>" + data.name + "</B>, How are you going today?&ensp;");
+      $("#welcomeText").html("Hey <B>" + data.name + "</B>, How is it going today?&ensp;");
       $("#welcome").show();
   },
   error: (resultat, statut, erreur) => {
diff --git a/projet/views/login.html b/projet/views/login.html
index 1791f9d..078e837 100644
--- a/projet/views/login.html
+++ b/projet/views/login.html
@@ -15,12 +15,12 @@
 
                       <div class="form-group">
                         <label for="username">Username</label>
-                        <input type="text" value="Aze" class="form-control" id="username">
+                        <input type="text" placeholder="username" class="form-control" id="username">
                       </div>
 
                       <div class="form-group">
                         <label for="pass">Password</label>
-                        <input type="password" value="aze" class="form-control" id="pass">
+                        <input type="password" placeholder="password" class="form-control" id="pass">
                       </div>
 
                       <button id="login" class="btn btn-primary" type="button">Login</button>
diff --git a/projet/views/register.html b/projet/views/register.html
index 83914ac..b87d740 100644
--- a/projet/views/register.html
+++ b/projet/views/register.html
@@ -17,18 +17,18 @@
 
                   <div class="form-group">
                     <label for="username">Username</label>
-                    <input type="text" class="form-control" id="username">
+                    <input type="text" class="form-control" id="username" placeholder="username">
                     <div id="conflict" style="color:brown"> Username already exist</div>
                   </div>
 
                   <div class="form-group">
                     <label for="name">Name</label>
-                    <input type="text" class="form-control" id="name">
+                    <input type="text" class="form-control" id="name" placeholder="name">
                   </div>
 
                   <div class="form-group">
                     <label for="pass">Password</label>
-                    <input type="password" class="form-control" id="pass">   
+                    <input type="password" class="form-control" id="pass" placeholder="password">   
                   </div>
 
                   <div class="form-group">
-- 
GitLab