1

I am trying to implement Passport.js Google Login to my MERN.

But, I have this middleware:

const jwt = require("jsonwebtoken");
const config = require("config");

module.exports = async function(req, res, next) {
  // Get token from header
  const token = req.header("x-auth-token");

  // Check if not token
  if (!token) {
    return res.status(401).json({ msg: "No token, authorization denied" });
  }

  // Verify token
  try {
    await jwt.verify(token, config.get("jwtSecret"), (error, decoded) => {
      if (error) {
        res.status(401).json({ msg: "Token is not valid" });
      } else {
        req.user = decoded.user;
        next();
      }
    });
  } catch (err) {
    console.error("something wrong with auth middleware");
    res.status(500).json({ msg: "Server Error" });
  }
};

As you can see, JWT is asking for token because I made JWT auth before.

What should I write on my Google Login callback? Should I write JWT on Callback? Does this make sense?

router.get(
  "/google/redirect",
  passport.authenticate("google"),
  async (req, res) => {
    //here
  }
);

1 Answers1

0

I had the same problem.

I found the suggestion: redirect to the expected page with a cookie which holds the JWT.

Link here: Facebook-passport with JWT

However, if we store the jwt token in a cookie, then we cannot use the api for the mobile app.