Invalid Memory Address with http.SetCookie

46 views Asked by At

I am working on an auth package called persona.

Every thing works fine except one thing, when I try to set a cookie, I have an invalid memory address.

func Signup(user interface{}, username string, w http.ResponseWriter) error {
    key := []byte(randStringBytes(32))

    encrypted, err := encrypt(key, username)
    if err != nil {
        return err
    }

    expiration := time.Now().Add(365 * 24 * time.Hour)
    cookie := http.Cookie{Name: "session-persona", Value: encrypted, Expires: expiration}
    http.SetCookie(w, &cookie)

    userSession := Session{Username: username, Key: key, Token: encrypted}
    database.Create(&userSession)

    database.Create(user)

    return nil
}

When I remove http.SetCookie(w, &cookie) everything works fine. Do you have any idea ?

1

There are 1 answers

0
Gniem On BEST ANSWER

I think the problem might not be the http.Cookie object, but perhaps the ResponseWriter object (since ResponseWriter is an interface). For debugging, try checking it for nil before setting the Cookie. If it is nil, you’ll probably have to look up the stack from `Signup’ to figure out why - perhaps it had already been closed.