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

Fix bug

parent 8b700fe7
No related branches found
No related tags found
No related merge requests found
......@@ -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;
......
......
......@@ -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;
......
......
......@@ -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) => {
......
......
......@@ -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>
......
......
......@@ -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">
......
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment