Skip to content

Cookies ​

Use cookies for small browser preferences that must survive another request. GoForj exposes Cookie and SetCookie on web.Context, using Go's http.Cookie. Auth owns its own session cookies. Do not overwrite auth_access or auth_refresh.

Read and Write a Preference ​

Add internal/browser/theme.go to an app with Web API enabled. This complete file defines helpers for a theme preference. ReadTheme treats the cookie as untrusted input and falls back to light.

go
package browser

import (
	"net/http"
	"time"

	"github.com/goforj/web"
)

// ReadTheme accepts only the theme names supported by the UI.
func ReadTheme(r web.Context) string {
	cookie, err := r.Cookie("theme")
	if err == nil && cookie.Value == "dark" {
		return "dark"
	}
	return "light"
}

// SetTheme stores a validated preference without exposing it to scripts.
func SetTheme(r web.Context, theme string, secure bool) error {
	if theme != "light" && theme != "dark" {
		return r.Text(http.StatusBadRequest, "invalid theme")
	}
	r.SetCookie(&http.Cookie{
		Name:     "theme",
		Value:    theme,
		Path:     "/",
		MaxAge:   30 * 24 * 60 * 60,
		Secure:   secure,
		HttpOnly: true,
		SameSite: http.SameSiteLaxMode,
	})
	return r.NoContent(http.StatusNoContent)
}

// ClearTheme expires the cookie at the same path used when setting it.
func ClearTheme(r web.Context, secure bool) error {
	r.SetCookie(&http.Cookie{
		Name:     "theme",
		Path:     "/",
		MaxAge:   -1,
		Expires:  time.Unix(1, 0),
		Secure:   secure,
		HttpOnly: true,
		SameSite: http.SameSiteLaxMode,
	})
	return r.NoContent(http.StatusNoContent)
}

Call ReadTheme(r) while rendering a page. Call SetTheme(r, "dark", true) or ClearTheme(r, true) from a state-changing handler protected by CSRF middleware. Pass false only for plain-HTTP local development. Set cookies before writing the response body.

Check that the helpers compile from the Project root.

bash
go test ./internal/browser

In the CSRF example, import your module's internal/browser package and return browser.SetTheme(r, "dark", false) from savePreference for local HTTP. Send the example's protected POST. Its 204 response sets theme=dark at /. With browser.ClearTheme(r, false) in the same handler, the 204 response expires the cookie. Inspect the Set-Cookie header in the browser's network panel.

Keep Secure enabled in production HTTPS deployments. HttpOnly prevents JavaScript from reading the cookie. Remove that flag only when browser code needs the preference and the value is safe to expose. SameSite=Lax limits cross-site submission but does not replace CSRF checks.

Omitting Domain keeps the cookie host-scoped. To delete an existing cookie, match its name, path, and domain. MaxAge is in seconds. A negative value deletes the cookie. Zero omits Max-Age and creates a browser-session cookie when no expiry is set.

Application cookies are not automatically encrypted or signed. Never trust a cookie value as a user ID, a permission, or a price. Keep sensitive state on the server and use the Auth session service for identity.