feat(api): localize console pages and api error messages
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
This commit is contained in:
1 parent
dde7b890b8
commit
70190446ff
12 files changed
+698
-211
No files matched your search
+24
-16
@@ -16,6 +16,7 @@ import (
|
||||
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/auth"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/config"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/i18n"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/store"
|
||||
"git.zb-server.de/ZB-Server/uptimesebi/internal/tenant"
|
||||
)
|
||||
@@ -46,12 +47,12 @@ func (s *Server) signup(c *gin.Context) {
|
||||
internalError(c, err)
|
||||
return
|
||||
} else if !allowed {
|
||||
fail(c, http.StatusForbidden, "signup_closed", "Registrierung ist deaktiviert.")
|
||||
fail(c, http.StatusForbidden, "signup_closed")
|
||||
return
|
||||
}
|
||||
hash, err := auth.HashPassword(req.Password)
|
||||
if errors.Is(err, auth.ErrPasswordTooShort) {
|
||||
fail(c, http.StatusUnprocessableEntity, "password_too_short", err.Error())
|
||||
fail(c, http.StatusUnprocessableEntity, "password_too_short", "Min", auth.MinPasswordLength)
|
||||
return
|
||||
} else if err != nil {
|
||||
internalError(c, err)
|
||||
@@ -66,6 +67,7 @@ func (s *Server) signup(c *gin.Context) {
|
||||
Email: normalizeEmail(req.Email),
|
||||
DisplayName: strings.TrimSpace(req.DisplayName),
|
||||
PasswordHash: &hash,
|
||||
Locale: ptr(i18n.Lang(ctx)),
|
||||
})
|
||||
if err != nil {
|
||||
return err
|
||||
@@ -77,19 +79,19 @@ func (s *Server) signup(c *gin.Context) {
|
||||
})
|
||||
switch {
|
||||
case isUniqueViolation(err, "users_email_key"):
|
||||
fail(c, http.StatusConflict, "email_taken", "Diese E-Mail-Adresse ist bereits registriert.")
|
||||
fail(c, http.StatusConflict, "email_taken")
|
||||
return
|
||||
case isUniqueViolation(err, "orgs_slug_key"):
|
||||
fail(c, http.StatusConflict, "slug_taken", "Dieser Kurzname ist bereits vergeben.")
|
||||
fail(c, http.StatusConflict, "slug_taken")
|
||||
return
|
||||
case isCheckViolation(err):
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_input", "Kurzname: 2–63 Zeichen, a–z, 0–9 und Bindestrich.")
|
||||
fail(c, http.StatusUnprocessableEntity, "invalid_slug")
|
||||
return
|
||||
case err != nil:
|
||||
internalError(c, err)
|
||||
return
|
||||
}
|
||||
s.startSession(c, user.ID, orgID)
|
||||
s.startSession(c, user.ID, orgID, i18n.Lang(ctx))
|
||||
}
|
||||
|
||||
func (s *Server) signupAllowed(ctx context.Context) (bool, error) {
|
||||
@@ -153,7 +155,7 @@ func (s *Server) login(c *gin.Context) {
|
||||
}
|
||||
|
||||
if user.TotpSecret != nil {
|
||||
token := randomToken()
|
||||
token := auth.RandomToken()
|
||||
if err := s.rdb.Set(ctx, mfaKey(token), user.ID.String(), mfaTTL).Err(); err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
@@ -177,7 +179,7 @@ func (s *Server) loginTOTP(c *gin.Context) {
|
||||
ctx := c.Request.Context()
|
||||
token, _ := c.Cookie(mfaCookie)
|
||||
if token == "" {
|
||||
fail(c, http.StatusUnauthorized, "mfa_expired", "Anmeldung abgelaufen, bitte erneut anmelden.")
|
||||
fail(c, http.StatusUnauthorized, "mfa_expired")
|
||||
return
|
||||
}
|
||||
if !s.limit(c, "mfa:"+mfaKey(token), 5, mfaTTL) {
|
||||
@@ -185,7 +187,7 @@ func (s *Server) loginTOTP(c *gin.Context) {
|
||||
}
|
||||
raw, err := s.rdb.Get(ctx, mfaKey(token)).Result()
|
||||
if err != nil {
|
||||
fail(c, http.StatusUnauthorized, "mfa_expired", "Anmeldung abgelaufen, bitte erneut anmelden.")
|
||||
fail(c, http.StatusUnauthorized, "mfa_expired")
|
||||
return
|
||||
}
|
||||
userID, err := uuid.Parse(raw)
|
||||
@@ -199,7 +201,7 @@ func (s *Server) loginTOTP(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
if !ok {
|
||||
fail(c, http.StatusUnauthorized, "invalid_code", "Code ungültig.")
|
||||
fail(c, http.StatusUnauthorized, "invalid_code")
|
||||
return
|
||||
}
|
||||
_ = s.rdb.Del(ctx, mfaKey(token)).Err()
|
||||
@@ -209,9 +211,15 @@ func (s *Server) loginTOTP(c *gin.Context) {
|
||||
|
||||
func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID) {
|
||||
ctx := c.Request.Context()
|
||||
var orgs []store.ListUserOrgsRow
|
||||
var (
|
||||
user store.User
|
||||
orgs []store.ListUserOrgsRow
|
||||
)
|
||||
err := s.db.Tx(ctx, tenant.User(userID), func(q *store.Queries) error {
|
||||
var err error
|
||||
if user, err = q.GetUser(ctx, userID); err != nil {
|
||||
return err
|
||||
}
|
||||
orgs, err = q.ListUserOrgs(ctx, userID)
|
||||
return err
|
||||
})
|
||||
@@ -223,14 +231,14 @@ func (s *Server) finishLogin(c *gin.Context, userID uuid.UUID) {
|
||||
if len(orgs) > 0 {
|
||||
org = orgs[0].ID
|
||||
}
|
||||
s.startSession(c, userID, org)
|
||||
s.startSession(c, userID, org, deref(user.Locale))
|
||||
}
|
||||
|
||||
func (s *Server) startSession(c *gin.Context, userID, orgID uuid.UUID) {
|
||||
func (s *Server) startSession(c *gin.Context, userID, orgID uuid.UUID, locale string) {
|
||||
if old := auth.Current(c); old != nil {
|
||||
_ = s.sessions.Delete(c.Request.Context(), old)
|
||||
}
|
||||
sess, err := s.sessions.Create(c.Request.Context(), userID, orgID)
|
||||
sess, err := s.sessions.Create(c.Request.Context(), userID, orgID, locale)
|
||||
if err != nil {
|
||||
internalError(c, err)
|
||||
return
|
||||
@@ -271,7 +279,7 @@ func (s *Server) limit(c *gin.Context, key string, n int, window time.Duration)
|
||||
}
|
||||
if !ok {
|
||||
c.Header("Retry-After", "60")
|
||||
fail(c, http.StatusTooManyRequests, "rate_limited", "Zu viele Versuche, bitte später erneut.")
|
||||
fail(c, http.StatusTooManyRequests, "rate_limited")
|
||||
return false
|
||||
}
|
||||
return true
|
||||
@@ -285,7 +293,7 @@ func mfaKey(token string) string {
|
||||
func normalizeEmail(e string) string { return strings.ToLower(strings.TrimSpace(e)) }
|
||||
|
||||
func invalidCredentials(c *gin.Context) {
|
||||
fail(c, http.StatusUnauthorized, "invalid_credentials", "E-Mail oder Passwort falsch.")
|
||||
fail(c, http.StatusUnauthorized, "invalid_credentials")
|
||||
}
|
||||
|
||||
func isUniqueViolation(err error, constraint string) bool {
|
||||
|
||||
Reference in new issue
Block a user