Nemok
Nemok

Reputation: 402

Redirect in nodejs using connect-auth

I am using nodejs 0.4.1 with Connect and connect-auth 0.3.0 for authentifications. When I login using any strategy I am doing a redirect.

The problem is that I am getting this error when writing headers for redirect (302): "Can't render headers after they are sent to the client.". It seems the headers have already been written.

req.authenticate(['google'], function(error, authenticated) 
        {
            if(error)
            {
                res.writeHead(302, {'Location': "/" + result.userName});
                res.end();
                return;
            }
            else if( authenticated === undefined)
            {
                return;
            }
            else if( authenticated ) 
            {
                sys.log("Authenticated from google " + util.inspect(req.getAuthDetails()));
                req.session.from = "google";
                RegisterOrLogin(...., function(result)
                {
                    if(result.error == false)//logged in successfuly
                    {//redirect
                        res.writeHead(302, {'Location': "/" + result.userName});
                        res.end();
                    }
                    else//could not log in
                    {
                        res.writeHead(302, {'Location': '/'});
                        res.end();
                    }
                });
                return;
            }
        });

Do you know of any workaround for this?

Thank you.

Upvotes: 0

Views: 1230

Answers (2)

ciaranj
ciaranj

Reputation: 537

That should work just fine. What happens to a lot of people is their configured redirects take them back to a host that was different to the one they left, so they get a different session which gives you the rather un-expected behaviour. Did you happen to catch the line number that was 're' writing the headers?

Upvotes: 0

Cris-O
Cris-O

Reputation: 5007

I was having a similar issue authenticating with FB. After hours messing with it, I switched to everyauth -https://github.com/bnoguchi/everyauth

works like a charm

Upvotes: 1

Related Questions