A
A
Alexander Yakovlev2022-02-22 20:51:40
JavaScript
Alexander Yakovlev, 2022-02-22 20:51:40

Why is the value not written to the Cookie?

When logging in, I store the jwt in a Cookie, but it is not written there if the user has been registered for a long time, and if the user has just registered and logged in, then the value is written.

The token itself comes, there are no errors. What could be the problem?

const serv = process.env.NODE_ENV === "development" ? "http://localhost:3000" : "";

import Cookies from "js-cookie";

export default {
  state() {
    return {
      token: null
    }
  },
  mutations: {
    setToken(state, val) {
      state.token = val;

      Cookies.set("token", val);
    },
  },
  actions: {
    async login({ commit }, fd) {
      try {
        const res = await fetch(`${serv}/auth/login`, {
          method: "POST",
          headers: {
            "Content-Type": "application/json",
            "Accept-Type": "application/json",
          },
          body: JSON.stringify(fd)
        });

        const data = await res.json();

        if (data.ok) {
          commit("setToken", data.token.replace(/^Bearer /, ""));
        }

        return data;
      } catch (err) {
        throw err;
      }
    },
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Volodymyr Palamar, 2022-02-22
@GORNOSTAY25

Try to specify the parameters. More specifically, the term 'expires'

Cookies.set('name', 'value', { expires: 7, path: '' })

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question