Next.js Discord

Discord Forum

Cookies().get Error.. please help

Unanswered
universe posted this in #help-forum
Open in Discord
Hello, I’ve managed to set up the authJwt, but I’m stuck on something kind of frustrating. For some reason, I can’t seem to grab these cookies from the headers when I make follow-up requests. Here’s my code:

"use server";
import axiosInstance from "@/lib/axiosInstance";
import { cookies } from "next/headers";

export const resendConfirmationEmail = async () => {
  try {

    const jwtToken = cookies().get("authJwt")?.value;

    if (!jwtToken) {
      console.error('JWT token not found in cookies.');
      return false;
    }

    const response = await axiosInstance.post('/api/auth/local/resend-confirmation',
      {},
      {
        headers: {
          Authorization: `Bearer ${jwtToken}`,
        },
        withCredentials: true, // HTTP-Only Cookie
      }
    );

    return response.status === 200;
  } catch (error) {
    console.error('Error resending email:', error);
    return false;
  }
};

14 Replies

Oh yes this one is the server action
can u show me your implementation
where u use your server action
"use client";
export default function ConfirmationMessage() {
  const [emailSent, setEmailSent] = useState(false);

  const resendEmail = async () => {
    const success = await resendConfirmationEmail();
    if (success) {
      setEmailSent(true);
    }
  };

return (...)

For this resend page!
The code I put on the post, that one is the resendConfirmation.
in that case that will not work
check how u properly use server actions
That means I need to separate the code? I'm the beginner of the web developing so.. Can you give me more specific advice?