Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passport authentication not working on Heroku?

My application is a nodejs web applicaiton using express. It serves as a portfolio website with a cms and api.

In my login route I have the following code:

passport.authenticate('local', function(err, user, info){
    if(err){
        return next(err);
    } else if(!user){
        res.format({
            json: function(){
                return error.throw403(req, res, next);
            },
            html: function(){
                return res.redirect('/login');
            }
        });
    } else {
        console.log('login route, user');
        console.log(user);
        req.logIn(user, function(err){
            if(err){
                return next(err);
            } else {
                res.format({
                    json: function(){
                        return res.send(user);
                    },
                    html: function(){
                        return res.redirect('/cms/users');
                    }
                });
            }
        });
    }
})(req, res, next);

My heroku logs tell me that user is defined and correct. The req.logIn() fires off, succeeds, and redirects (as it should) to /cms/users. However, I get a 403 at /cms/users because req.user is not in the request. This makes me think that my sessions are not persisting, but I'm no expert and have no clue what to do next.

I'm using redistogo and mongolab, and both configs were set using the CLI. Any suggestions? I'm stuck! Thanks.

EDIT: I should add that everything is working fine on my local machine.

like image 655
reagan Avatar asked Dec 06 '25 06:12

reagan


1 Answers

Documentation is where I should have started.

https://devcenter.heroku.com/articles/redistogo#using-with-node-js

I followed every configuration on that site to the letter and then tweaked app configuration order until it worked.

like image 130
reagan Avatar answered Dec 08 '25 20:12

reagan