how can modify cookies in route handler with server action
let say we have page route
export defualt function Page(){ const user=await getUser(); if(!user){ /* serversignout is server action which implements a another page */ serversignout(); redirect("/login"); } return <div>my page</div>}
export defualt function Page(){ const user=await getUser(); if(!user){ /* serversignout is server action which implements a another page */ serversignout(); redirect("/login"); } return <div>my page</div>}
server signout is implemented as given below
"use server"//necessary importsexport async function serversignout() { const cookiesStore = cookies(); const csrf = cookiesStore.get("next-auth.csrf-token"); cookies().set({ name: "next-auth.csrf-token", value: "", expires: new Date("2016-10-05"), path: "/", // For all paths }); cookies().set({ name: "next-auth.callback-url", value: "", expires: new Date("2016-10-05"), path: "/", // For all paths }); cookies().set({ name: "next-auth.session-token", value: "", expires: new Date("2016-10-05"), path: "/", // For all paths }); console.log(csrf); await fetch(absoluteUrl("/api/auth/signout"), { cache: "no-cache", method: "POST", body: JSON.stringify({ csrfToken: csrf?.value }), });}
"use server"//necessary importsexport async function serversignout() { const cookiesStore = cookies(); const csrf = cookiesStore.get("next-auth.csrf-token"); cookies().set({ name: "next-auth.csrf-token", value: "", expires: new Date("2016-10-05"), path: "/", // For all paths }); cookies().set({ name: "next-auth.callback-url", value: "", expires: new Date("2016-10-05"), path: "/", // For all paths }); cookies().set({ name: "next-auth.session-token", value: "", expires: new Date("2016-10-05"), path: "/", // For all paths }); console.log(csrf); await fetch(absoluteUrl("/api/auth/signout"), { cache: "no-cache", method: "POST", body: JSON.stringify({ csrfToken: csrf?.value }), });}