The language comes from the user setting, then the lang cookie, then Accept-Language, falling back to English. Errors keep a stable code and carry a translated message. Closes #42 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
83 lines
2.0 KiB
Go
83 lines
2.0 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("console.monitors.title", web.Dashboard))
|
|
r.GET("/settings/security", s.page("security.title", web.Security))
|
|
}
|
|
|
|
func (s *Server) pageFor(c *gin.Context, titleID string) web.Page {
|
|
p := web.Page{TitleID: titleID, Languages: s.catalog.Supported()}
|
|
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(s.pageFor(c, "auth.login.title"), 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(s.pageFor(c, "auth.signup.title")))
|
|
}
|
|
|
|
func (s *Server) page(titleID 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(s.pageFor(c, titleID), 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
|
|
}
|