#how to set the payload cookie/token on payload.login in 3.0 with Next JS and server actions

1 messages · Page 1 of 1 (latest)

wispy ingot
#

I was looking at the docs where in 2.0 you pass an express server response into the object like so:

await payload.login({
  collection: 'users',
    data: {
      email,
      password,
    },
    res: res
})

But how do I do the same in 3.0 using server actions with Next Response?

fallow oasisBOT
wispy ingot
#

how to set the payload cookie/token on payload.login in 3.0 with Next JS and server actions

dusty lion
#

Hey @wispy ingot have you got any solution for this? I'm finf the same

#

@livid flame can you answer this?

reef moth
# dusty lion Hey <@436119970439495682> have you got any solution for this? I'm finf the same

this is my signin form action

export async function signIn(_: any, formData: FormData): Promise<ActionResult> {
  "use server";
  const email = formData.get("email") as string;
  const password = formData.get("password") as string;

  const payload = await getFullPayload()

  try {
    const user = await payload.login({
      collection: 'users',
      data: {
        email,
        password
      },
      

    })

    cookies().set({
      name: "payload-token",
      value: user.token,
      path: "/",
      expires: new Date(user.exp * 1000),
    });

  } catch (e) {

    if (e.name === "ValidationError") {
      return {
        error: "validation_error",
      };
    }

    if (e.name === "AuthenticationError") {
      return {
        error: "authentication_error",
      };
    }

    if (e.name === "LockedAuth") {
      return {
        error: "locked",
      };
    }

    return {
      error: "unknown_error",
    };
  }

  return redirect("/");
}
#

should do what you need