Can't overwrite a cookie value creates a second with the same name?

I’m trying to update the value of a cookie that is set so that a function I have will send the user to a login place. Basically a logout function. I can’t figure out why the value of my cookie value isn’t overwriting.

What I’m seeing is that a second cookie with the same name is being created in Chrome, and the cookie is not changing in FireFox.

totally confused… (image from Chrome dev tools at the bottom of the page)

package controller

import (
	"fmt"
	"html/template"
	"net/http"

	"github.com/nasquam/taskmaster/model"
)

type userLogOut struct {
	userLogOutTemplate *template.Template
}

func (h userLogOut) registerRoutes() {
	http.HandleFunc("/logout", h.handleUserLogOut)
	http.HandleFunc("/logout/", h.handleUserLogOut)
}

func (h userLogOut) handleUserLogOut(w http.ResponseWriter, r *http.Request) {
	fmt.Println("In userLogOut handleUserLogOut")
	model.DeleteAuthTokenFromCookie(w)
	model.CheckCookies(r)
	//http.Redirect(w, r, "/login/", 301)
}
func DeleteAuthTokenFromCookie(w http.ResponseWriter) {
	
	fmt.Println("In DeleteAuthTokenFromCookie Function")
	tokenCookie := http.Cookie{Name: "authValue", Value: "deleted"}
	http.SetCookie(w, &tokenCookie)
}
func CheckCookies(r *http.Request) {
	EmailCookie, _ := r.Cookie("emailValue")
	TokenCookie, _ := r.Cookie("authValue")

	fmt.Print("Email value from CheckCookies is: ")
	fmt.Println(EmailCookie)
	fmt.Print("Token value from CheckCookies is: ")
	fmt.Println(TokenCookie)
}

to delete a cookie you can another variable maxAge

It was actually the “Path” option that I was missing.

okay. But this is another simple way to do it.
func logout(w http.ResponseWriter, r *http.Request) { // MaxAge=0 means no 'Max-Age' attribute specified. // MaxAge<0 means delete cookie now, equivalently 'Max-Age: 0' // MaxAge>0 means Max-Age attribute present and given in seconds http.SetCookie(w, &http.Cookie{ Name: "authValue", MaxAge: -1, }) http.Redirect(w, r, "/", http.StatusSeeOther) }
good to know how to solve your errors.

That is a way, but that wasn’t the issue I was experiencing. In Chrome there was a second cookie being created because the source page was not “/”. The cookie was being set from “/logout”. So a second cookie was being created with the “logout” path.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.