Next.js Discord

Discord Forum

Managing Cookies

Answered
Barbary Lion posted this in #help-forum
Open in Discord
Barbary LionOP
When a user proceeds to checkout, a Stripe checkout is being created and the session id is being stored in the cookies.

Once the payment is completed, they get redirected back to my success page and I want to delete the session id from the cookies but I get the error message, that it's only possible in route handlers and server actions.

I'm not that familiar with server actions and working with cookies.

What is the best practice to work with cookies?
Answered by chisto
you make a new file with the directive "use server" on the top so this functions only run in the server
something like this
'use server'
 
export async function deleteCookies() {
...your logic goes here...
}

and then you use that function

assuming you are using CheckoutForm component from stripe and you have their functions too like stripe.confirmPayment
you can do something like
const { error, paymentIntent } = await stripe.confirmPayment()
if (paymentIntent && paymentIntent.status === 'succeeded') {
await deleteCookies()
}
View full answer

2 Replies

you make a new file with the directive "use server" on the top so this functions only run in the server
something like this
'use server'
 
export async function deleteCookies() {
...your logic goes here...
}

and then you use that function

assuming you are using CheckoutForm component from stripe and you have their functions too like stripe.confirmPayment
you can do something like
const { error, paymentIntent } = await stripe.confirmPayment()
if (paymentIntent && paymentIntent.status === 'succeeded') {
await deleteCookies()
}
Answer
Barbary LionOP
Oh alright, ty