lavender/server/home.go

47 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"
2024-02-15 15:09:14 +00:00
"time"
2024-02-07 01:18:17 +00:00
)
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")
2024-02-15 15:09:14 +00:00
lNonce := uuid.NewString()
http.SetCookie(rw, &http.Cookie{
Name: "tulip-nonce",
Value: lNonce,
Path: "/",
Expires: time.Now().Add(10 * time.Minute),
Secure: true,
SameSite: http.SameSiteLaxMode,
})
2024-02-07 01:18:17 +00:00
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) {
2024-02-15 15:09:14 +00:00
roles, err := tx.GetUserRoles(auth.ID)
2024-02-12 20:58:49 +00:00
isAdmin = HasRole(roles, "lavender:admin")
return err
})
2024-02-07 01:18:17 +00:00
pages.RenderPageTemplate(rw, "index", map[string]any{
"ServiceName": h.conf.ServiceName,
"Auth": auth,
2024-02-15 15:09:14 +00:00
"Subject": auth.ID,
"DisplayName": auth.DisplayName,
2024-02-07 01:18:17 +00:00
"Nonce": lNonce,
2024-02-12 20:58:49 +00:00
"IsAdmin": isAdmin,
2024-02-07 01:18:17 +00:00
})
}