Skip to content
Snippets Groups Projects
Commit 1862aa4a authored by justin.foltz's avatar justin.foltz
Browse files

Fix bug

parent 8b700fe7
Branches master
No related tags found
No related merge requests found
...@@ -156,7 +156,7 @@ checkToken = async (id, token) => { ...@@ -156,7 +156,7 @@ checkToken = async (id, token) => {
const containsId = await containId(id); const containsId = await containId(id);
if(containsId){ if(containsId){
const user = await Profil.findOne({ "_id" : id }).select('+token'); 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 true;
} }
return false; return false;
......
...@@ -11,15 +11,16 @@ app.use(bodyParser.urlencoded({ extended: true })); ...@@ -11,15 +11,16 @@ app.use(bodyParser.urlencoded({ extended: true }));
require('dotenv').config(); require('dotenv').config();
const db = require("./DB/DB.js"); 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 // Make sure that the user token is valid
const auth = async(req, res, next) => { let auth = async(req, res, next) => {
try { try {
const token = req.header('Authorization').replace('Bearer ', ''); const token = req.header('Authorization').replace('Bearer ', '');
const data = jwt.verify(token, process.env.JWT_SECRET); 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(); throw new Error();
} }
req.userId = data.userId; req.userId = data.userId;
......
...@@ -90,6 +90,7 @@ function getFavoriteEvents() { ...@@ -90,6 +90,7 @@ function getFavoriteEvents() {
}, },
error: (resultat, statut, erreur) => { error: (resultat, statut, erreur) => {
if( erreur === "Unauthorized") { if( erreur === "Unauthorized") {
alert("getFavoriteEvents");
window.location = "/login"; window.location = "/login";
} }
} }
...@@ -169,6 +170,7 @@ function getEvents() { ...@@ -169,6 +170,7 @@ function getEvents() {
url: URL, url: URL,
headers: jsonHeader(), headers: jsonHeader(),
success: (data) => { success: (data) => {
console.log("DATA get event", data);
storeToken(data.token); storeToken(data.token);
if(data.events) { if(data.events) {
data.events.forEach( event => { data.events.forEach( event => {
...@@ -180,6 +182,7 @@ function getEvents() { ...@@ -180,6 +182,7 @@ function getEvents() {
}, },
error: (resultat, statut, erreur) => { error: (resultat, statut, erreur) => {
if( erreur === "Unauthorized") { if( erreur === "Unauthorized") {
alert("getEvents");
window.location = "/login"; window.location = "/login";
} }
} }
...@@ -305,7 +308,7 @@ $.ajax({ ...@@ -305,7 +308,7 @@ $.ajax({
headers: jsonHeader(), headers: jsonHeader(),
success: (data) => { success: (data) => {
storeToken(data.token); 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(); $("#welcome").show();
}, },
error: (resultat, statut, erreur) => { error: (resultat, statut, erreur) => {
......
...@@ -15,12 +15,12 @@ ...@@ -15,12 +15,12 @@
<div class="form-group"> <div class="form-group">
<label for="username">Username</label> <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>
<div class="form-group"> <div class="form-group">
<label for="pass">Password</label> <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> </div>
<button id="login" class="btn btn-primary" type="button">Login</button> <button id="login" class="btn btn-primary" type="button">Login</button>
......
...@@ -17,18 +17,18 @@ ...@@ -17,18 +17,18 @@
<div class="form-group"> <div class="form-group">
<label for="username">Username</label> <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 id="conflict" style="color:brown"> Username already exist</div>
</div> </div>
<div class="form-group"> <div class="form-group">
<label for="name">Name</label> <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>
<div class="form-group"> <div class="form-group">
<label for="pass">Password</label> <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>
<div class="form-group"> <div class="form-group">
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment