#Server actions

1 messages · Page 1 of 1 (latest)

fallen pumiceBOT
#

Hi @green osprey. I'm kapa.ai, your friendly support bot. I will try to answer your question by searching through the knowledge base. :wave:

#

The issue you're experiencing might be due to the fact that you're setting the cookie with the user data before updating the user's emailVerified status in your database. When you call JSON.stringify(user), it serializes the user object as it is at that moment, which does not include the updated emailVerified status.

Here's the corrected version of your code:

export async function EmailAction(userToken: string) {
    const user = await prisma.user.findFirst({
        where: {
            token: userToken
        }
    });

    if(!Boolean(user) || user === null) {
        return "Invalid token"
    }

    const updatedUser = await prisma.user.update({
        where: {
            token: userToken
        },
        data: {
            emailVerified: true
        }
    })

    const cookieResponse = cookies();
    cookieResponse.set("user", JSON.stringify(updatedUser), {path: "/", maxAge: 3600});
    cookieResponse.set("token", updatedUser.token, {path: "/", maxAge: 3600});
    
    return "success";
}

In this updated version, the prisma.user.update method is expected to return the updated user object, which is then used to set the cookie. This ensures that the cookie contains the most up-to-date user data.

Remember, the .set() method for cookies is only available in a Server Action (https://nextjs.org/docs/app/building-your-application/data-fetching/server-actions) or Route Handler (https://nextjs.org/docs/app/building-your-application/routing/router-handlers) as per the Next.js documentation (https://nextjs.org/docs/app/api-reference/functions/cookies).

Note: you can ask me a follow up question by @ mentioning me again :speech_balloon: