DEV Community

Rakib Ahsan
Rakib Ahsan

Posted on

"Cachec - no store" Bug Next Js 14

Recently, I attempted to log in using JWT authentication. However, I encountered an issue where, upon logging in and storing the JWT in a cookie, sometimes the system would return an outdated token. This proved to be problematic as I had already deleted the previous token.

Despite employing techniques such as using a fetch request with no cache, I still encountered unsatisfactory results. Subsequently, I delved into their documentation further and explored potential solutions.

`export async function Login(data: { email: string; password: string }) {
  try {
    noStore();//this work as force dynamic and no store
    const res = await fetch(`${baseURL}/login`, {
      method: "POST",
      headers: {
        "Content-Type": "application/json",
        cache: "no-store",
      },
      body: JSON.stringify(data),
    });
    const responseData = await res.json();

    console.log(responseData);

    if (responseData.token) {
      cookies().set("token", responseData.token, {
        httpOnly: true,
        expires: new Date("2030-01-01"),
      });
    }

    return responseData;
  } catch (error) {
    console.error("Error:", error);
    throw error;
  }
}`
Enter fullscreen mode Exit fullscreen mode

next js doc here

Top comments (0)