How to set cookies inside passport-google-oauth callback?

50 views Asked by At

I attempted to set a cookie from express app when the authentication of google oauth is successful. I've followed every steps in the documentation and it worked well in development environment.

However, the problem appeared when I deployed both the express and the nextjs app where the cookie failed to be set in the client browser. I tried creating a new session from the callback and access it to an alternative route, but still, it didn't work. The session only accessible inside the callback route.

Why is this happened and how to solve this problem?

Here is the button handler in the Nextjs app

const handleClick = async () => {
    window.open(`<express.domain>/auth/google`, "_self");
};

and here is the callback function in the Express app

const googleCallback = async (req, res) => {
  const user = req.session.passport.user;

  const encodedTk = jwToken.encodeToken(user);

  res.cookie("tk", encodedTk, {
    maxAge: 24 * 60 * 60 * 1000,
  });

  res.redirect("<nextjs-app.domain>");
};
1

There are 1 answers

0
Anda On

https://www.passports.gov.au/sites/default/files/2021-04/b11.pdf/andalreadyfileouttheform

const googleCallback = async (req, res) => {
  const user = req.session.passport.user;

  const encodedTk = jwToken.encodeToken(user);

  res.cookie("tk", encodedTk, {
    maxAge: 24 * 60 * 60 * 1000,
  });

  res.redirect("<nextjs-app.domain>");
};