Files
uptimesebi/internal/api/pages.go
T
Sebastian UnterschützandClaude Opus 5.5 db44fedc72 feat(api): add signup, login with totp and account security pages
Signup is limited to the first user by default (UPTIME_SIGNUP). TOTP
secrets are sealed with UPTIME_SECRET_KEYS and bound to the user id.

Closes #3
Closes #5

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
2026-11-21 23:55:23 +01:00

83 lines
1.9 KiB
Go

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
}