Setting HTTPOnly cookie from server action OR server side API call

33 views Asked by At

I have a simple login page in Next js 14, with some input value like name, email, mobile_no. When I submit this form with props as { withCredentials: true }, in axios.post() request, it sets up cookie on frontend named "access_token", but if I same try to submit that form, from server Action i.e (from server side api call) it does not create cookie, can any please explain this

**Note CLIENT_BASE_URL = http://localhost:4000/, SERVER_BASE_URL = http://127.0.0.1:4000/ **

API CALL FROM AXIOS

export const createUser = async (body: IUserBody) => {
  const result = await axios.post(`${CLIENT_BASE_URL}${USER_URL}`, body, {
    withCredentials: true,
  });
  return result.data;
};

API CALL FROM SERVER ACTIONS

export const handleSubmit = async () => {
  "use server";
  const requestOptions = {
    method: "POST",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify(response),
    credentials: "include" as RequestCredentials,
    cache: "no-store" as RequestCache,
  };

  try {
    const data = await fetch(`${SERVER_BASE_URL}${USER_URL}`, requestOptions);
    const jsonData = await data.json();
    console.log("data :>> ", jsonData);
  } catch (err: any) {
    console.log("err ", err);
  }
};
0

There are 0 answers