title: How can I save my token after closing the browser so that I stay logged in?
body: <p>I have a React application that allows users to login using either google auth or email and password. In both cases, a JWT token containing the user ID is created. As long as the user has the respective browser open, he can reload the page without having to log in again. However, if the user closes the window, he must log in again because the token is no longer available. How can I change this ?</p>
  <p>This is my auth controller class inside my server:</p>
  <pre><code>export const googleAuth = async (req, res, next) =&gt; {
  try {
  const user = await User.findOne({ email: req.body.email });
  if (user) {
    console.log(user);
    const token = jwt.sign({ id: user._id }, process.env.JWT);
    res
      .cookie(&quot;access_token&quot;, token, {
        httpOnly: true,
      })
      .status(200)
      .json(user._doc);
  } else {
    console.log(req.body);
    const newUser = new User({
      ...req.body,
      fromGoogle: true,
    });
    const savedUser = await newUser.save();
    const token = jwt.sign({ id: savedUser._id }, process.env.JWT);
    res
      .cookie(&quot;access_token&quot;, token, {
        httpOnly: true,
      })
      .status(200)
      .json(savedUser._doc);
   }
    } catch (err) {
   next(err);
   }
  };
  </code></pre>
  <p>This is my verifyToken.js class, which checks whether the user is still logged in or whether the token is still available</p>
  <pre><code>export const verifyToken = (req, res, next) =&gt; {
  const token = req.cookies.access_token;
  
   if (!token) return next(createError(401, &quot;You are not authenticated!&quot;));
  
   jwt.verify(token, process.env.JWT, (err, user) =&gt; {
   if (err) return next(createError(403, &quot;Token is not valid!&quot;));
    req.user = user;
    next();
   });
  };
  </code></pre>
  <p>And that is calling a route, which first checks the verifiyToken and then, if it exists, executes the method</p>
  <pre><code>import { verifyToken } from &quot;../verifyToken.js&quot;;
  const router = express.Router();
  router.get(&quot;/fav&quot;, verifyToken, fav);
  
  
  
  const loadArticel = async () =&gt; {
  setLoading(true);
  await axiosInstance
    .get(`/articel/fav`, { withCredentials: true })
    .then((res) =&gt; {
      setArticel(res.data);
      setLoading(false);
    });
   };
  </code></pre>
  <p>However, this is the result:</p>
  <p><a href="https://i.stack.imgur.com/8K6ds.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8K6ds.jpg" alt="enter image description here" /></a></p>