hoangvvo / nextjs-mongodb-app

A Next.js and MongoDB web application, designed with simplicity for learning and real-world applicability in mind.
https://nextjs-mongodb.now.sh/
MIT License
1.54k stars 289 forks source link

facebook login (Need help to fix) #78

Closed IRediTOTO closed 4 years ago

IRediTOTO commented 4 years ago

I am trying to implement Facebook login, it's work pretty fine. User can login with FB, server got profile with user's email > then create or authenticate user,... But something wrong in api/user/facebook/callback it's didn't make user session login. And that make user got kick out of profile page. Sometimes it work (on localhost), sometime can't login.

vercel[bot] commented 4 years ago

This pull request is being automatically deployed with Vercel (learn more). To see the status of your deployment, click below or on the icon next to each commit.

IRediTOTO commented 4 years ago

Hmm, i found error. Session id change every request :| finding the way to fix it.

hoangvvo commented 4 years ago

I'm not sure that I'm following, but here is my guess.

In pages/api/user/facebook/index.js, you need to import passport from lib/passport.js, not the passport module itself.

IRediTOTO commented 4 years ago

@hoangvvo I tried, all passport is ok. problem come from

res.writeHead(301, {  this should be 302 
         Location: process.env.WEB_URI + "/account",
      });
res.end();

If I just req.send("ok") it will work

hoangvvo commented 4 years ago

Sorry for the late reply. I'm not sure why 302 would make a difference.

IRediTOTO commented 4 years ago

@hoangvvo Hi, is use this, and it work

      res.setHeader("Location", process.env.WEB_URI + "/user");
      res.statusCode=302
      res.end();

Look like res.writeHead destroy something.

hoangvvo commented 4 years ago

I understood. It seems like next-session could not write the Cookie header because writeHead had sent out the headers before it got to do so.

https://nodejs.org/api/http.html#http_response_writehead_statuscode_statusmessage_headers

Closing. If there is still a problem, feel free to reopen.