E
E
Elena2020-11-09 18:16:50
JavaScript
Elena, 2020-11-09 18:16:50

How to get cookies from browser?

Is there a function that checks if the user is logged in 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;
};

During authentication, cookies are stored in the browser,
5fa95cf97b07d794890970.png
but for some reason it still says that you need to log in. Moreover, cookies are stored in the postman and the user can log in. Tell me what's the problem? Maybe I'm making the 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

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yuriev, 2020-11-09
@Elena0394

Because you have cookies with the HttpOnly flag

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question