passport / discuss

A forum for discussing topics related to the usage of Passport.js.
1 stars 0 forks source link

Cookie not sent for immediate redirect request after login with passport #58

Open pradosh987 opened 2 years ago

pradosh987 commented 2 years ago

Not sure if this is a issue with passport or not but still posting here. I have simple authentication setup with passport google oauth strategy. It works quite well with one exception. After getting callback, it sets a cookie (using session-cookie) and does redirect to homepage. But cookies are not sent for this immediate redirect request. If I reload again or go to any page, cookies are sent properly and I am logged in properly.

Here's the callback code for redirect

router.get(
    "/google/callback",
    passport.authenticate("google", { failureRedirect: "/login" }),
    function (req, res) {
      res.redirect("/");
    }
  );

Any idea what might be cause??? posted this on stack overflow too https://stackoverflow.com/questions/70269844/cookie-not-sent-for-immediate-redirect-request-after-login-with-passport

pradosh987 commented 2 years ago

Found workaround for this based on what shopify is doing. Instead of redirecting I am sending below html which redirects to desired page with the cookies

res.contentType("text/html").send(`<html>
        <head>
          <title></title>
          <script>
            (function(){window.location = "/"})()
          </script>
        </head>

        <body>
          <p>Logging in...</p>
        </body>

        </html>`);

Keeping this open because I am not sure why this was happening In first place. Feel free to close it at your discretion.