Preserve safe return paths through sign-in
CI / test (push) Failing after 3m18s

This commit is contained in:
2026-09-16 05:36:36 +02:00
parent b6d264c9ec
commit 87809b2344
8 changed files with 116 additions and 6 deletions
+13 -1
View File
@@ -43,7 +43,7 @@ func (app *application) recoverPanic(next http.Handler) http.Handler {
func (app *application) requireAuthentication(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !app.isAuthenticated(r) {
http.Redirect(w, r, webPath("login"), http.StatusSeeOther)
http.Redirect(w, r, loginPathForRequest(r), http.StatusSeeOther)
return
}
w.Header().Set("Cache-Control", "no-store")
@@ -51,6 +51,18 @@ func (app *application) requireAuthentication(next http.Handler) http.Handler {
})
}
func loginPathForRequest(r *http.Request) string {
loginPath := webPath("login")
if r.Method != http.MethodGet {
return loginPath
}
returnTo := safeReturnPath(r.URL.RequestURI())
if returnTo == "" {
return loginPath
}
return pathWithQuery(loginPath, "return_to", returnTo)
}
func (app *application) requireActivatedUser(next http.Handler) http.Handler {
return app.requireAuthentication(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
user, ok := userFromContext(r.Context())