Add multi language support for authentication middleware

This commit is contained in:
Achim Rohn
2025-09-17 18:05:23 +02:00
parent 667345bc9e
commit f70a4ad777
10 changed files with 182 additions and 10 deletions
+21 -5
View File
@@ -78,10 +78,10 @@ func LogoutSession(writer http.ResponseWriter, request *http.Request, sessionSto
http.Redirect(writer, request, redirectUrl, http.StatusTemporaryRedirect)
}
func Middleware(store *sessions.CookieStore, excludedPrefixes []string, redirectUrl string) MiddlewareFunc {
func Middleware(store *sessions.CookieStore, excludedPrefixes []string, redirect LanguagePaths) MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Debug("Authenticating")
Debug("Authenticating for page", r.URL.Path)
if r.Method == http.MethodOptions {
next.ServeHTTP(w, r)
return
@@ -92,16 +92,32 @@ func Middleware(store *sessions.CookieStore, excludedPrefixes []string, redirect
return
}
}
ok, r, err := SetUserIdAndEmailFromSessionStore(r, store)
for _, path := range redirect {
if strings.HasPrefix(r.URL.Path, path) {
next.ServeHTTP(w, r)
return
}
}
ok, newR, err := SetUserIdAndEmailFromSessionStore(r, store)
if err != nil {
LogError("Failed to set user id and email from session: %v", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
if ok {
next.ServeHTTP(w, r)
next.ServeHTTP(w, newR)
} else {
Debug("Redirecting to login because there is no session")
redirectUrl := En.GetPath() + redirect[En]
for lang, path := range redirect {
if strings.HasPrefix(r.URL.Path, lang.GetPath()) {
redirectUrl = lang.GetPath() + path
break
}
}
if redirectUrl == "" {
redirectUrl = "/"
}
Debug("Redirecting to ", redirectUrl, "because there is no session")
http.Redirect(w, r, redirectUrl, http.StatusTemporaryRedirect)
}
})
+4 -1
View File
@@ -23,7 +23,10 @@ func NewLoginPage(e *echo.Echo, createPage ersteller_lib.CreatePageFunc, googleL
func (l *LoginPage) Render(c echo.Context) error {
return l.createPage(c, ersteller_lib.PageWebsiteMetaData{
Title: "Login",
Title: ersteller_lib.NewI18nText(map[ersteller_lib.Language]string{
ersteller_lib.En: "Login",
ersteller_lib.De: "Anmelden",
}),
HideNavigation: true,
}, l.getLoginPage())
}