tulip/cmd/red-tulip/server/home.go

52 lines
1.3 KiB
Go
Raw Normal View History

2024-01-29 23:45:46 +00:00
package server
import (
"fmt"
2024-01-29 23:45:46 +00:00
"github.com/1f349/tulip/cmd/red-tulip/pages"
"github.com/1f349/tulip/database"
"github.com/google/uuid"
"github.com/julienschmidt/httprouter"
"net/http"
)
func (h *HttpServer) Home(rw http.ResponseWriter, req *http.Request, _ httprouter.Params, auth UserAuth) {
rw.Header().Set("Content-Type", "text/html")
rw.WriteHeader(http.StatusOK)
if auth.IsGuest() {
2024-01-29 23:45:46 +00:00
pages.RenderPageTemplate(rw, "index-guest", map[string]any{
2023-10-10 18:06:43 +01:00
"ServiceName": h.conf.ServiceName,
})
return
}
lNonce := uuid.NewString()
auth.Session.Set("action-nonce", lNonce)
if auth.Session.Save() != nil {
http.Error(rw, "Failed to save session", http.StatusInternalServerError)
return
}
var userWithName *database.User
2023-11-25 15:08:44 +00:00
var hasTwoFactor bool
if h.DbTx(rw, func(tx *database.Tx) (err error) {
userWithName, err = tx.GetUserDisplayName(auth.Data.ID)
if err != nil {
return fmt.Errorf("failed to get user display name: %w", err)
}
2023-11-25 15:08:44 +00:00
hasTwoFactor, err = tx.HasTwoFactor(auth.Data.ID)
if err != nil {
return fmt.Errorf("failed to get user two factor state: %w", err)
}
return
}) {
return
}
2024-01-29 23:45:46 +00:00
pages.RenderPageTemplate(rw, "index", map[string]any{
2023-10-10 18:06:43 +01:00
"ServiceName": h.conf.ServiceName,
"Auth": auth,
"User": userWithName,
"Nonce": lNonce,
2023-11-25 15:08:44 +00:00
"OtpEnabled": hasTwoFactor,
})
}