98 lines
2.2 KiB
Go
98 lines
2.2 KiB
Go
package auth
|
|
|
|
import (
|
|
"dk/internal/router"
|
|
)
|
|
|
|
// FlashMessage represents a flash message with type and content
|
|
type FlashMessage struct {
|
|
Type string `json:"type"` // "error", "success", "warning", "info"
|
|
Message string `json:"message"`
|
|
}
|
|
|
|
// SetFlashMessage sets a flash message for the current session
|
|
func SetFlashMessage(ctx router.Ctx, msgType, message string) bool {
|
|
sessionID := GetSessionCookie(ctx)
|
|
if sessionID == "" {
|
|
return false
|
|
}
|
|
|
|
return Manager.SetFlash(sessionID, "message", FlashMessage{
|
|
Type: msgType,
|
|
Message: message,
|
|
})
|
|
}
|
|
|
|
// GetFlashMessage retrieves and removes the flash message from the current session
|
|
func GetFlashMessage(ctx router.Ctx) *FlashMessage {
|
|
sessionID := GetSessionCookie(ctx)
|
|
if sessionID == "" {
|
|
return nil
|
|
}
|
|
|
|
value, exists := Manager.GetFlash(sessionID, "message")
|
|
if !exists {
|
|
return nil
|
|
}
|
|
|
|
if msg, ok := value.(FlashMessage); ok {
|
|
return &msg
|
|
}
|
|
|
|
// Handle map[string]interface{} from JSON deserialization
|
|
if msgMap, ok := value.(map[string]interface{}); ok {
|
|
msg := &FlashMessage{}
|
|
if t, ok := msgMap["type"].(string); ok {
|
|
msg.Type = t
|
|
}
|
|
if m, ok := msgMap["message"].(string); ok {
|
|
msg.Message = m
|
|
}
|
|
return msg
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// SetFormData stores form data temporarily in the session (for repopulating forms after errors)
|
|
func SetFormData(ctx router.Ctx, data map[string]string) bool {
|
|
sessionID := GetSessionCookie(ctx)
|
|
if sessionID == "" {
|
|
return false
|
|
}
|
|
|
|
return Manager.SetSessionData(sessionID, "form_data", data)
|
|
}
|
|
|
|
// GetFormData retrieves and removes form data from the session
|
|
func GetFormData(ctx router.Ctx) map[string]string {
|
|
sessionID := GetSessionCookie(ctx)
|
|
if sessionID == "" {
|
|
return nil
|
|
}
|
|
|
|
value, exists := Manager.GetSessionData(sessionID, "form_data")
|
|
if !exists {
|
|
return nil
|
|
}
|
|
|
|
// Clear form data after retrieval
|
|
Manager.SetSessionData(sessionID, "form_data", nil)
|
|
|
|
if formData, ok := value.(map[string]string); ok {
|
|
return formData
|
|
}
|
|
|
|
// Handle map[string]interface{} from JSON deserialization
|
|
if formMap, ok := value.(map[string]interface{}); ok {
|
|
result := make(map[string]string)
|
|
for k, v := range formMap {
|
|
if str, ok := v.(string); ok {
|
|
result[k] = str
|
|
}
|
|
}
|
|
return result
|
|
}
|
|
|
|
return nil
|
|
} |