lavender/server/home.go

94 lines
2.6 KiB
Go
Raw Normal View History

2024-02-07 01:18:17 +00:00
package server
import (
auth2 "github.com/1f349/lavender/auth"
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"
2024-09-13 15:31:40 +01:00
"github.com/1f349/lavender/role"
2024-02-07 01:18:17 +00:00
"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, req *http.Request, _ httprouter.Params, auth auth2.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{
2024-02-21 09:16:38 +00:00
Name: "lavender-nonce",
2024-02-15 15:09:14 +00:00
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.Queries) (err error) {
2024-09-13 15:31:40 +01:00
err = tx.UserHasRole(req.Context(), database.UserHasRoleParams{Role: role.LavenderAdmin, Subject: auth.Subject})
2024-09-02 22:54:03 +01:00
isAdmin = err == nil
return nil
2024-02-12 20:58:49 +00:00
})
2024-02-07 01:18:17 +00:00
pages.RenderPageTemplate(rw, "index", map[string]any{
"ServiceName": h.conf.ServiceName,
"Auth": auth,
"Nonce": lNonce,
2024-02-12 20:58:49 +00:00
"IsAdmin": isAdmin,
2024-02-07 01:18:17 +00:00
})
// rw.Header().Set("Content-Type", "text/html")
// 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,
// })
//
// if auth.IsGuest() {
// pages.RenderPageTemplate(rw, "index-guest", map[string]any{
// "ServiceName": h.conf.ServiceName,
// })
// return
// }
//
// var userWithName string
// var userRole types.UserRole
// var hasTwoFactor bool
// if h.DbTx(rw, func(tx *database.Queries) (err error) {
// userWithName, err = tx.GetUserDisplayName(req.Context(), auth.Subject)
// if err != nil {
// return fmt.Errorf("failed to get user display name: %w", err)
// }
// hasTwoFactor, err = tx.HasOtp(req.Context(), auth.Subject)
// if err != nil {
// return fmt.Errorf("failed to get user two factor state: %w", err)
// }
// userRole, err = tx.GetUserRole(req.Context(), auth.Subject)
// if err != nil {
// return fmt.Errorf("failed to get user role: %w", err)
// }
// return
// }) {
// return
// }
// pages.RenderPageTemplate(rw, "index", map[string]any{
// "ServiceName": h.conf.ServiceName,
// "Auth": auth,
// "User": database.User{Subject: auth.Subject, Name: userWithName, Role: userRole},
// "Nonce": lNonce,
// "OtpEnabled": hasTwoFactor,
// "IsAdmin": userRole == types.RoleAdmin,
// })
2024-02-07 01:18:17 +00:00
}