How do I get cookies from the browser?

  • 0
    There is a function that checks whether the user is authorized or not.
    module.exports = (req) => {
      console.log(req.cookies.jwt); // undefined
      if (!req.cookies.jwt) {
        throw new Error('Необходима авторизация');
      let payload;
      try {
        payload = jwt.verify(req.cookies.jwt, JWT_SECRET);
      } catch (e) {
        throw new Error('Необходима авторизация');
      req.user = payload;

    When authenticated, cookies are saved in the browser.

    but for some reason still writes that you need to log in. Moreover, in the postman, cookies are saved and the user can log in. Tell me what is the problem? Maybe I'm making a request wrong?
    getUserInfo = () => {
      return fetch('http://localhost:3000/users/me')
        .then(res => res.ok ? res.json() : Promise.reject(res.status))
        .catch(err => console.log(`Ошибка: ${err}`))

    neither credentials: 'include' nor credentials: 'same-origin' helps
    JavaScript Anonymous, Feb 23, 2019

  • 1 Answers
  • 0
    Because you have cookies with the flag HttpOnly
    Victoria Juarez

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!