stew/* Packages / 14.8

๐Ÿช stew/cookies

The stew/cookies package facilitates the management of client-side cookies (sessions, preferences) without having to manually parse the document.cookie string.

Simple Reading and Writing

Manage basic cookies with a single line of Go code.

<goscript client>
    import "stew/cookies"

    saveTheme := func(name string) {
        cookies.Set("theme", name, cookies.Options{Path: "/"})
    }

    deleteSession := func() {
        cookies.Delete("session_id")
    }
</goscript>

Advanced Options

Define expiration dates, domains, or enable SSL security for your cookies.

<goscript client>
    import "stew/cookies"
    import "time"

    cookies.Set("consent", "true", cookies.Options{
        Expires: time.Now().AddDate(1, 0, 0), // Expires in 1 year
        Secure:  true,
        SameSite: "Strict",
    })
</goscript>

Verification Utilities

Quickly check for the existence of a cookie or access the complete list.

<goscript client>
    import "stew/cookies"

    if cookies.Has("user_id") {
        username := cookies.Get("username")
    }

    all := cookies.GetAll() // map[string]string
</goscript>
๐Ÿ”’ Cookie Security

If your cookies contain sensitive data (session tokens), ensure they are marked as Secure and SameSite=Lax/Strict. Note that HttpOnly can NOT be enabled by the client JS/Wasm.