From 7db142b39c5cf1fba315e2c17cc26345304d8ffd Mon Sep 17 00:00:00 2001 From: Anirudh Oppiliappan Date: Tue, 28 Jan 2025 12:42:22 +0200 Subject: [PATCH] return to / if already logged in at /login --- legit/routes/auth.go | 7 +++++++ legit/routes/handler.go | 7 +++++-- legit/routes/routes.go | 4 ++-- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/legit/routes/auth.go b/legit/routes/auth.go index b3089eb..66e7ea8 100644 --- a/legit/routes/auth.go +++ b/legit/routes/auth.go @@ -20,6 +20,7 @@ func (h *Handle) AuthMiddleware(next http.Handler) http.Handler { auth, ok := session.Values["authenticated"].(bool) if !ok || !auth { + log.Printf("not logged in, redirecting") http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) return } @@ -60,6 +61,12 @@ func (h *Handle) AuthMiddleware(next http.Handler) http.Handler { log.Println("successfully refreshed token") } + if r.URL.Path == "/login" { + log.Println("already logged in") + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) + return + } + next.ServeHTTP(w, r) }) } diff --git a/legit/routes/handler.go b/legit/routes/handler.go index ca60df6..a19b221 100644 --- a/legit/routes/handler.go +++ b/legit/routes/handler.go @@ -60,8 +60,11 @@ func Setup(c *config.Config) (http.Handler, error) { auth: auth, } - r.Get("/login", h.Login) - r.Post("/login", h.Login) + r.Group(func(r chi.Router) { + r.Use(h.AuthMiddleware) + r.Get("/login", h.Login) + r.Post("/login", h.Login) + }) r.Get("/static/{file}", h.ServeStatic) r.Route("/repo", func(r chi.Router) { diff --git a/legit/routes/routes.go b/legit/routes/routes.go index 5aa2870..845517a 100644 --- a/legit/routes/routes.go +++ b/legit/routes/routes.go @@ -463,8 +463,8 @@ func (h *Handle) Login(w http.ResponseWriter, r *http.Request) { } log.Printf("successfully saved session for %s (%s)", atSession.Handle, atSession.Did) - w.Header().Set("HX-Redirect", "/") - w.WriteHeader(http.StatusOK) + http.Redirect(w, r, "/", http.StatusPermanentRedirect) + return } } -- 2.51.2