Documentation
¶
Overview ¶
Package flash provides utilities for one-time flash messages using cookies. Flash messages are commonly used to show feedback after form submissions or redirects (e.g., "Registration successful!" or "Invalid credentials").
Index ¶
- Constants
- func ClearPending(w http.ResponseWriter)
- func Error(w http.ResponseWriter, message string)
- func Get(r *http.Request, w http.ResponseWriter, key string) string
- func GetError(r *http.Request, w http.ResponseWriter) string
- func GetSuccess(r *http.Request, w http.ResponseWriter) string
- func IsPending(r *http.Request) bool
- func RedirectWithError(w http.ResponseWriter, r *http.Request, redirectURL, message string)
- func RedirectWithSuccess(w http.ResponseWriter, r *http.Request, redirectURL, message string)
- func Set(w http.ResponseWriter, key, message string)
- func SetPending(w http.ResponseWriter)
- func Success(w http.ResponseWriter, message string)
- type Messages
Constants ¶
const ( // ErrorKey is the cookie name for error flash messages ErrorKey = "flash_error" // SuccessKey is the cookie name for success flash messages SuccessKey = "flash_success" // PendingKey marks that a flash message was just set (for one-time display) PendingKey = "flash_pending" // DefaultMaxAge is how long flash cookies live (30 seconds) // This allows time for slow redirects or network delays DefaultMaxAge = 30 )
Variables ¶
This section is empty.
Functions ¶
func ClearPending ¶
func ClearPending(w http.ResponseWriter)
ClearPending clears the pending marker.
func Error ¶
func Error(w http.ResponseWriter, message string)
Error sets an error flash message.
Example:
flash.Error(w, "Invalid email or password")
func Get ¶
Get reads and clears a flash message. Returns empty string if no flash message exists.
Example:
if msg := flash.Get(r, w, "error"); msg != "" {
// Display error message
}
func GetError ¶
func GetError(r *http.Request, w http.ResponseWriter) string
GetError reads and clears the error flash message.
Example:
if errMsg := flash.GetError(r, w); errMsg != "" {
state.FlashError = errMsg
}
func GetSuccess ¶
func GetSuccess(r *http.Request, w http.ResponseWriter) string
GetSuccess reads and clears the success flash message.
Example:
if successMsg := flash.GetSuccess(r, w); successMsg != "" {
state.FlashSuccess = successMsg
}
func RedirectWithError ¶
func RedirectWithError(w http.ResponseWriter, r *http.Request, redirectURL, message string)
RedirectWithError redirects to a URL with an error flash message.
Example:
flash.RedirectWithError(w, r, "/auth", "Invalid credentials")
func RedirectWithSuccess ¶
func RedirectWithSuccess(w http.ResponseWriter, r *http.Request, redirectURL, message string)
RedirectWithSuccess redirects to a URL with a success flash message.
Example:
flash.RedirectWithSuccess(w, r, "/", "Welcome back!")
func Set ¶
func Set(w http.ResponseWriter, key, message string)
Set sets a flash message cookie with the given key. The message is URL-encoded to handle special characters.
Example:
flash.Set(w, "info", "Your session will expire soon")
func SetPending ¶
func SetPending(w http.ResponseWriter)
SetPending marks that a flash message was just set. This is used to distinguish between a fresh flash and a page reload.
func Success ¶
func Success(w http.ResponseWriter, message string)
Success sets a success flash message.
Example:
flash.Success(w, "Account created successfully!")