#Debugging: User Null Issue in Password Endpoint

2 messages · Page 1 of 1 (latest)

blazing ploverBOT
#

I have such endpoint

export const changePassword = async ({
  currentPassword,
  newPassword,
}: {
  currentPassword: string;
  newPassword: string;
}) => {
  const response = await fetch(`${env.PUBLIC_URL}/api/users/me/password`, {
    method: "POST",
    credentials: "include",
    headers: {
      "Content-Type": "application/json",
    },
    body: JSON.stringify({ currentPassword, newPassword }),
  });
  const result = await response.json();

  return result;
};

but in endpoint im getting user:null , i got a payload-token in cookies, what i do wrong?

export const changePassword: Endpoint = {
  method: "post",
  path: "/me/password",
  handler: async (req) => {
    const { currentPassword, newPassword } = await req.json?.();

    console.log(req.user);
    if (!req.user) {
      return Response.json({ message: "Not authenticated" }, { status: 400 });
    }

    const payload = req.payload;
    const user = await payload.findByID({
      collection: "users",
      id: req.user.id,
    });

    const response = await login({
      collection: "users",
      config: payload.config,
      email: user.email,
      password: currentPassword,
    });

    if (!response.token) {
      return Response.json(
        { message: "Current password is incorrect" },
        { status: 400 }
      );
    }

    await payload.update({
      collection: "users",
      id: req.user.id,
      data: { password: newPassword },
    });

    return Response.json({ message: "Password updated" }, { status: 200 });
  },
};


olive wingBOT
#

Original message from @glossy ether - Moved from #general message