Error in nodejs [closed]

2

I am trying to do the following. I have the endpoint sgts.

/
/secret
/user/login  //Post-Get
/user/logout //Post
/user/register //Post-Get
/user/recuperar //Post-Get

I have 2 unlogged and logged middlewares

Here are my routes:

router.use(logged.unlogged);
router.use("/logout",logged.logged);
router.route("/login").get(login.login_get).post(login.login_post);
router.route("/recuperar").get(recuperar.recuperar_get).post(recuperar.recuperar_post);
router.route("/registrar").get(registrar.registrar_get).post(registrar.registrar_post);
router.route("/logout").get(logout);

I want you to grant me access to those endpoints when it is not logged in, and when it is logged in it does not allow me access to them, so I put those 2 middlewares.

These are my middlewares

var logged=function(req,res,next){
    if(req.session.user_id!=undefined){
        next();
    }
    else{
        console.log(req.session);
        res.redirect("/");
    }
}

var unlogged=function(req,res,next){
    if(req.session.user_id!=undefined) res.redirect("/user/login");
    else{
        next();
    }
}


module.exports={
    logged:logged,
    unlogged:unlogged
}

Once I login and put my password and password, find the user and redirect me to the endpoint / password which is only activated if the person is logged in.

User.findOne({userName:req.body.userName,password:req.body.password},function(err,us){
    if(err)console.log(String(err));
    console.log("Usuario encontra\n",us._id);
    if(us){
        req.session.user_id=us._id;
        console.log("Se redirigira a password");
        res.redirect("/password");
    }

    res.redirect("/");

});

But I get the following error

Can not set headers after ther are sent , but I do not understand it well, says that I can not change the headers after I send them, I do not see my error.

Now, I do not know if the way I'm doing what I want is the right way, or there is another lighter way to do what I want.

From stackoverflow in English, what I understood is possibly the error in the middleware because res.redirect modifies the headers, but it is not entirely clear to me.

EDITING The error is removed by giving a return to all the redirects involved, but it is not clear to me why the return is necessary, I should also put in all, another question is whether there is an easier way to execute what I try to do.

    
asked by Kevin AB 25.09.2016 в 18:36
source

1 answer

0

The first error is in the first middleware router.use(logged.unlogged); since this means that once the user authenticates he will not be able to access any endpoint since he will be redirected to /user/login as specified by this middleware. For endpoints that it is necessary that the user has not logged in before, it is best to include this middleware in that specific route and not in a general way.

In fact, the correct middlewares would be:

var logged=function(req,res,next){
    if(req.session.user_id!=undefined){
        next();
    }
    else{
        console.log(req.session);
        res.redirect("/user/login");
    }
}

var unlogged=function(req,res,next){
    if(req.session.user_id!=undefined) res.redirect("/");
    else{
        next();
    }
}
    
answered by 28.09.2016 в 20:07