next 13 cookies

"use server";

import { UsersApi } from "../crud/user.api";
import apiFetch from "@/utils/api/shared/apiFetch";
import { cookies } from "next/headers";

export default async function verifySignIn({ userID }: { userID: string }) {
const cookieStore = cookies();

let userJwt = cookieStore.get("tempAuthToken")?.value;
if (!userJwt) throw new Error("Something wrong happened. Please try again.");

try {
const { data, authHeaders } = await apiFetch<string[], object>("POST", `/users/${userID}/verify`, { userJwt });

if (authHeaders) {
cookieStore.set("authToken", authHeaders);
cookieStore.delete("tempAuthToken");
}

if (data[0]) {
await new UsersApi().update(userID, { active: true });
}

return data[0];
} catch (error) {
throw error;
}
}
"use server";

import { UsersApi } from "../crud/user.api";
import apiFetch from "@/utils/api/shared/apiFetch";
import { cookies } from "next/headers";

export default async function verifySignIn({ userID }: { userID: string }) {
const cookieStore = cookies();

let userJwt = cookieStore.get("tempAuthToken")?.value;
if (!userJwt) throw new Error("Something wrong happened. Please try again.");

try {
const { data, authHeaders } = await apiFetch<string[], object>("POST", `/users/${userID}/verify`, { userJwt });

if (authHeaders) {
cookieStore.set("authToken", authHeaders);
cookieStore.delete("tempAuthToken");
}

if (data[0]) {
await new UsersApi().update(userID, { active: true });
}

return data[0];
} catch (error) {
throw error;
}
}
Im trying to set cookies in next 13 using this server action function, but im getting this error. I'm already using a server action, why cant i set and delete?
No description
1 Reply
IMESSE
IMESSE10mo ago
I think that in order to make it a server action you should mark it with “use server” inside the function