lavender/server/home.go

43 lines
1.1 KiB
Go
Raw Normal View History

2024-02-07 01:18:17 +00:00
package server
import (
2024-02-12 20:58:49 +00:00
"github.com/1f349/lavender/database"
2024-02-07 01:18:17 +00:00
"github.com/1f349/lavender/pages"
"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
"net/http"
)
func (h *HttpServer) Home(rw http.ResponseWriter, _ *http.Request, _ httprouter.Params, auth UserAuth) {
2024-02-07 01:18:17 +00:00
rw.Header().Set("Content-Type", "text/html")
if auth.IsGuest() {
pages.RenderPageTemplate(rw, "index-guest", map[string]any{
"ServiceName": h.conf.ServiceName,
})
return
}
2024-02-12 20:58:49 +00:00
var isAdmin bool
h.DbTx(rw, func(tx *database.Tx) (err error) {
roles, err := tx.GetUserRoles(auth.Data.ID)
isAdmin = HasRole(roles, "lavender:admin")
return err
})
2024-02-07 01:18:17 +00:00
lNonce := uuid.NewString()
auth.Session.Set("action-nonce", lNonce)
if auth.Session.Save() != nil {
http.Error(rw, "Failed to save session", http.StatusInternalServerError)
return
}
pages.RenderPageTemplate(rw, "index", map[string]any{
"ServiceName": h.conf.ServiceName,
"Auth": auth,
"Subject": auth.Data.ID,
"DisplayName": auth.Data.DisplayName,
"Nonce": lNonce,
2024-02-12 20:58:49 +00:00
"IsAdmin": isAdmin,
2024-02-07 01:18:17 +00:00
})
}