Add multi language support for authentication middleware
This commit is contained in:
+21
-5
@@ -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)
|
||||
}
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user