1
Good night!
Working on a project, and on the login screen after performing the authentication (Passportjs), the user will be redirected to the home page.
However, a little while ago I started to adopt some new methods to better organize the structure of the program etc. And I am leaving a file only for routes (Routes) and another only to manage the calls GET, POST, DELETE, PATCH, PUT (controllers).
Before migrating the files to this new format, everything worked normally... In this piece of code I’m in trouble... In this format everything works normally
app.get("/home", isAuth, (req, res) => {
res.render("home", {
name: req.user.username
});
});
isAuth is a module to check whether the user is authenticated or not, and if so, will be redirected.
module.exports = {
isAuth: (req, res, next) => {
if(req.isAuthenticated()) {
return next();
} else {
req.flash("error_msg", "Please, log in to view this page");
res.redirect("/login");
}
}
}
In this format I get the error "Cannot GET /home"
// @desc Home Page
// @route GET /home
// @acess Private
exports.getHome = (isAuth),(req, res) => {
res.render("home", {
name: req.user.username
});
};
Can anyone tell me what I’m doing wrong?
How are you importing this new module? And how are you connecting it to Express?
– Luiz Felipe
It seems to me that the error is at the time you export. Try to export an array:
exports.getHome = [isAuth, (req, res) => {
 res.render("home", {
 name: req.user.username
 });
}]
.– Luiz Felipe
I am importing in this way: const { isAuth } = require(".. /config/auth");
– r. duarte