filegate/utils/metacontent.go

55 lines
1.1 KiB
Go

package utils
import (
"fmt"
"time"
"github.com/gin-contrib/sessions"
"github.com/gin-gonic/gin"
)
type MetaContent struct {
Username string
IsLoggedIn bool
IsAdmin bool
Timestamp string
ErrorTitle string
ErrorText string
}
// MetaContent collects all the values needed to render the templates that aren't specific to the view
// and combines them into a structure expected by the template
func GenMetaContent(c *gin.Context) MetaContent {
session := sessions.Default(c)
sessionUserName := session.Get("username")
sessionIsLoggedIn := session.Get("isLoggedIn")
sessionIsAdmin := session.Get("isAdmin")
var username string
var isLoggedIn bool
var isAdmin bool
// Validate Types
if sessionUserName != nil {
username, _ = sessionUserName.(string)
} else {
username = ""
}
if sessionIsLoggedIn != nil {
isLoggedIn, _ = sessionIsLoggedIn.(bool)
} else {
isLoggedIn = false
}
if sessionIsAdmin != nil {
isAdmin, _ = sessionIsAdmin.(bool)
} else {
isAdmin = false
}
return MetaContent{
Username: username,
IsLoggedIn: isLoggedIn,
IsAdmin: isAdmin,
Timestamp: fmt.Sprintf("%d", time.Now().Year()),
}
}