diff --git a/projet/DB/DB.js b/projet/DB/DB.js index 5237f05fe87a200fa3c06dc2e35a83d9c387faca..94146d37d79ca374c1e5cee5c7101ee4a2a7f4b0 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 8f4a6d3c20f975d0bd95edf526f18d0f914cfc98..074741a15680086b7aaa65de3dc86ab2262a133f 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 028304f865544eb0f4f473281c639c8c9da6cb3f..096b60fa7afdfd4a4bef992132eac3cd0efa84f0 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? "); + $("#welcomeText").html("Hey <B>" + data.name + "</B>, How is it going today? "); $("#welcome").show(); }, error: (resultat, statut, erreur) => { diff --git a/projet/views/login.html b/projet/views/login.html index 1791f9d128195e0077e73c299a8dcfc4c89d6b4e..078e83772d30490cb7f9c13fbd6d3d8a467fe88f 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 83914ac5dafc7b8d3d03558ae7f31dbd9956a376..b87d740538e5dcc0c8f919855754815ce8869634 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">