i did something
This commit is contained in:
@@ -74,7 +74,7 @@ exports.signin = (req, res) => {
|
||||
}
|
||||
|
||||
if (!user) {
|
||||
return res.status(404).send({ message: "User Not found." });
|
||||
return res.status(404).send({ message: "rip utilisateur introuvable" });
|
||||
}
|
||||
|
||||
var passwordIsValid = bcrypt.compareSync(
|
||||
@@ -85,9 +85,9 @@ exports.signin = (req, res) => {
|
||||
if (!passwordIsValid) {
|
||||
return res.status(401).send({
|
||||
accessToken: null,
|
||||
message: "Invalid Password!"
|
||||
message: "oof! mauvais mot de passe"
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
var token = jwt.sign({ id: user.id }, config.secret, {
|
||||
expiresIn: 86400 // 24 hours
|
||||
@@ -103,7 +103,8 @@ exports.signin = (req, res) => {
|
||||
username: user.username,
|
||||
email: user.email,
|
||||
roles: authorities,
|
||||
accessToken: token
|
||||
accessToken: token,
|
||||
message: "utilisateur authentifié"
|
||||
});
|
||||
});
|
||||
};
|
||||
@@ -1,15 +1,12 @@
|
||||
exports.allAccess = (req, res) => {
|
||||
res.status(200).send("Public Content.");
|
||||
res.status(200).send("ljdp.ca");
|
||||
};
|
||||
|
||||
exports.userBoard = (req, res) => {
|
||||
res.status(200).send("User Content.");
|
||||
res.status(200).send("bienvenue "+req.userId);
|
||||
console.log(req.userId);
|
||||
};
|
||||
|
||||
exports.adminBoard = (req, res) => {
|
||||
res.status(200).send("Admin Content.");
|
||||
};
|
||||
|
||||
exports.moderatorBoard = (req, res) => {
|
||||
res.status(200).send("Moderator Content.");
|
||||
res.status(200).send("admin");
|
||||
};
|
||||
@@ -8,12 +8,12 @@ verifyToken = (req, res, next) => {
|
||||
let token = req.headers["x-access-token"];
|
||||
|
||||
if (!token) {
|
||||
return res.status(403).send({ message: "No token provided!" });
|
||||
return res.status(403).send({ message: "token introuvable :*(" });
|
||||
}
|
||||
|
||||
jwt.verify(token, config.secret, (err, decoded) => {
|
||||
if (err) {
|
||||
return res.status(401).send({ message: "Unauthorized!" });
|
||||
return res.status(401).send({ message: "non autorisé lol" });
|
||||
}
|
||||
req.userId = decoded.id;
|
||||
next();
|
||||
@@ -51,40 +51,8 @@ isAdmin = (req, res, next) => {
|
||||
});
|
||||
};
|
||||
|
||||
isModerator = (req, res, next) => {
|
||||
User.findById(req.userId).exec((err, user) => {
|
||||
if (err) {
|
||||
res.status(500).send({ message: err });
|
||||
return;
|
||||
}
|
||||
|
||||
Role.find(
|
||||
{
|
||||
_id: { $in: user.roles }
|
||||
},
|
||||
(err, roles) => {
|
||||
if (err) {
|
||||
res.status(500).send({ message: err });
|
||||
return;
|
||||
}
|
||||
|
||||
for (let i = 0; i < roles.length; i++) {
|
||||
if (roles[i].name === "moderator") {
|
||||
next();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
res.status(403).send({ message: "Require Moderator Role!" });
|
||||
return;
|
||||
}
|
||||
);
|
||||
});
|
||||
};
|
||||
|
||||
const authJwt = {
|
||||
verifyToken,
|
||||
isAdmin,
|
||||
isModerator
|
||||
};
|
||||
module.exports = authJwt;
|
||||
@@ -10,18 +10,12 @@ module.exports = function(app) {
|
||||
next();
|
||||
});
|
||||
|
||||
app.get("/api/test/all", controller.allAccess);
|
||||
app.get("/api/all", controller.allAccess);
|
||||
|
||||
app.get("/api/test/user", [authJwt.verifyToken], controller.userBoard);
|
||||
app.get("/api/user", [authJwt.verifyToken], controller.userBoard);
|
||||
|
||||
app.get(
|
||||
"/api/test/mod",
|
||||
[authJwt.verifyToken, authJwt.isModerator],
|
||||
controller.moderatorBoard
|
||||
);
|
||||
|
||||
app.get(
|
||||
"/api/test/admin",
|
||||
"/api/admin",
|
||||
[authJwt.verifyToken, authJwt.isAdmin],
|
||||
controller.adminBoard
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user