captainbuckkets
captainbuckketsā€¢3mo ago

Removing cookie for basic logout

I seem to be losing my mind. My logout pathway used to work, but now no matter what method I use, the cookie remains and a) the cookie is never removed b) the user can't actually logout as a consequence Here is a simple pathway using both the .88 STD methods, and the Oak library
deleteCookie(response, "token")
cookies.delete("token", "");
await cookies.set("token", "",)

// response.status = 302;
// response.headers.set("Location", "/");
response.redirect("/login")
deleteCookie(response, "token")
cookies.delete("token", "");
await cookies.set("token", "",)

// response.status = 302;
// response.headers.set("Location", "/");
response.redirect("/login")
3 Replies
captainbuckkets
captainbuckketsā€¢3mo ago
This did previously work and somehow in the last week, its stopped functioning as it used to
Deno AI Helper
Deno AI Helperā€¢3mo ago
It looks like you're trying to delete a cookie in a few different ways, but you're encountering issues with the cookie not being removed. Let's go through the process step by step using the standard library (std) methods, as described in the documentation. Here's a snippet from the documentation that shows how to properly delete a cookie:
import { Handlers } from "$fresh/server.ts";
import { deleteCookie } from "std/http/cookie.ts";

export const handler: Handlers = {
GET(req) {
const url = new URL(req.url);
const headers = new Headers(req.headers);
deleteCookie(headers, "auth", { path: "/", domain: url.hostname });

headers.set("location", "/");
return new Response(null, {
status: 302,
headers,
});
},
};
import { Handlers } from "$fresh/server.ts";
import { deleteCookie } from "std/http/cookie.ts";

export const handler: Handlers = {
GET(req) {
const url = new URL(req.url);
const headers = new Headers(req.headers);
deleteCookie(headers, "auth", { path: "/", domain: url.hostname });

headers.set("location", "/");
return new Response(null, {
status: 302,
headers,
});
},
};
In this example, the deleteCookie function from the std/http/cookie.ts module is used to delete the "auth" cookie. The deleteCookie function takes three arguments: 1. headers: The headers object where the Set-Cookie header will be set to delete the cookie. 2. name: The name of the cookie to delete. 3. attributes: An object with attributes for the cookie. In this case, we're specifying the path and domain to ensure the cookie is deleted for the correct scope. After deleting the cookie, the code sets the location header to redirect the user to the root path ("/"), and it returns a Response object with a status of 302 to indicate a redirect. Make sure that when you're deleting the cookie, you're specifying the correct path and domain attributes that match the cookie you're trying to delete. If these don't match, the cookie may not be deleted as expected. Also, ensure that you're using the headers object from the request or creating a new Headers object to pass to
Deno AI Helper
Deno AI Helperā€¢3mo ago
deleteCookie, and then use this same headers object to set the location for the redirect. If you're still having trouble, you might want to check the following: - Verify that the cookie is being set with the correct domain and path when you initially create it. - Ensure that the response with the Set-Cookie header to delete the cookie is actually being sent back to the client. - Check if there's any client-side JavaScript that might be interfering with the cookie deletion. - Use the browser's developer tools to inspect the network response and see if the Set-Cookie header is present and correctly configured to instruct the browser to delete the cookie. Remember, the deleteCookie function should modify the headers to instruct the client's browser to remove the cookie. If you're using a framework like Oak, make sure to follow the framework's specific methods for manipulating cookies and sending responses. šŸ¦•