cookie

package
v0.1.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 21, 2026 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package cookie provides utilities for setting and clearing HTTP cookies with secure defaults appropriate for authentication and session management.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clear

func Clear(w http.ResponseWriter, name string)

Clear clears a cookie by setting MaxAge to -1.

Example:

cookie.Clear(w, "session")

func ClearLiveTemplateSession

func ClearLiveTemplateSession(w http.ResponseWriter)

ClearLiveTemplateSession clears the LiveTemplate session cookie. This forces a fresh session state on the next page load, which is necessary after logout to ensure the home page shows the correct logged-out state.

Example:

// In logout handler
cookie.Clear(w, "users_token")
cookie.ClearLiveTemplateSession(w)
http.Redirect(w, r, "/", http.StatusSeeOther)

func ClearSecure

func ClearSecure(w http.ResponseWriter, name string)

ClearSecure clears a secure cookie by setting MaxAge to -1. Matches the attributes used by SetSecure.

Example:

cookie.ClearSecure(w, "session")

func Get

func Get(r *http.Request, name string) string

Get retrieves a cookie value by name. Returns empty string if cookie doesn't exist.

Example:

token := cookie.Get(r, "session")
if token == "" {
    // Not logged in
}

func IsSecure

func IsSecure(r *http.Request) bool

IsSecure returns true if the request is over HTTPS. It checks both the URL scheme and common proxy headers.

func Set

func Set(w http.ResponseWriter, name, value string, maxAge int)

Set sets a cookie with secure defaults. Uses HttpOnly and SameSite=Lax for security.

Example:

cookie.Set(w, "preference", "dark", 86400) // 1 day

func SetSecure

func SetSecure(w http.ResponseWriter, name, value string, maxAge int)

SetSecure sets a cookie with strict security settings. Uses HttpOnly, Secure, and SameSite=Strict. Use this for sensitive cookies like session tokens.

Example:

cookie.SetSecure(w, "session", token, 30*24*60*60) // 30 days

func SetSession

func SetSession(w http.ResponseWriter, r *http.Request, name, value string, maxAgeDays int)

SetSession sets a session cookie with strict security settings. The maxAgeDays parameter specifies how long the session should last. Uses HttpOnly and SameSite=Strict for authentication cookies. The Secure flag is automatically set based on the request protocol.

Example:

cookie.SetSession(w, r, "users_token", token, 30) // 30 days

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL