package api import ( "net/http" "github.com/a-h/templ" "github.com/gin-gonic/gin" "git.zb-server.de/ZB-Server/uptimesebi/internal/auth" "git.zb-server.de/ZB-Server/uptimesebi/internal/web" ) func (s *Server) RegisterPages(r gin.IRouter) { r.GET("/login", s.loginPage) r.GET("/signup", s.signupPage) r.GET("/", s.page("Übersicht", web.Dashboard)) r.GET("/settings/security", s.page("Sicherheit", web.Security)) } func pageFor(c *gin.Context, title string) web.Page { p := web.Page{Title: title} if sess := auth.Current(c); sess != nil { p.CSRF = sess.CSRF } return p } func (s *Server) loginPage(c *gin.Context) { if auth.Current(c) != nil { c.Redirect(http.StatusSeeOther, "/") return } allowed, err := s.signupAllowed(c.Request.Context()) if err != nil { internalError(c, err) return } web.Render(c, http.StatusOK, web.Login(pageFor(c, "Anmelden"), allowed)) } func (s *Server) signupPage(c *gin.Context) { allowed, err := s.signupAllowed(c.Request.Context()) if err != nil { internalError(c, err) return } if !allowed { c.Redirect(http.StatusSeeOther, "/login") return } web.Render(c, http.StatusOK, web.Signup(pageFor(c, "Konto anlegen"))) } func (s *Server) page(title string, view func(web.Page, web.Me) templ.Component) gin.HandlerFunc { return func(c *gin.Context) { sess := auth.Current(c) if sess == nil { c.Redirect(http.StatusSeeOther, "/login") return } me, err := s.loadMe(c.Request.Context(), sess) if err != nil { internalError(c, err) return } web.Render(c, http.StatusOK, view(pageFor(c, title), toWebMe(me))) } } func toWebMe(m meView) web.Me { out := web.Me{ Email: m.Email, DisplayName: m.DisplayName, TOTP: m.TOTP, ActiveOrg: m.ActiveOrg.String(), TOTPMissing: m.TOTPEnrollmentRequired, } for _, o := range m.Orgs { out.Orgs = append(out.Orgs, web.Org{ID: o.ID.String(), Name: o.Name, Slug: o.Slug, IsOwner: o.IsOwner}) } return out }