Mirror of @tangled.org/core. Running on a Raspberry Pi Zero 2 (Please be gentle).

fix redirect in follow/star buttons

+15 -5
+15 -5
appview/state/middleware.go
··· 21 21 func AuthMiddleware(s *State) Middleware { 22 22 return func(next http.Handler) http.Handler { 23 23 return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { 24 + redirectFunc := func(w http.ResponseWriter, r *http.Request) { 25 + http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 26 + } 27 + if r.Header.Get("HX-Request") == "true" { 28 + redirectFunc = func(w http.ResponseWriter, _ *http.Request) { 29 + w.Header().Set("HX-Redirect", "/login") 30 + w.WriteHeader(http.StatusOK) 31 + } 32 + } 33 + 24 34 session, err := s.auth.GetSession(r) 25 35 if session.IsNew || err != nil { 26 36 log.Printf("not logged in, redirecting") 27 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 37 + redirectFunc(w, r) 28 38 return 29 39 } 30 40 31 41 authorized, ok := session.Values[appview.SessionAuthenticated].(bool) 32 42 if !ok || !authorized { 33 43 log.Printf("not logged in, redirecting") 34 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 44 + redirectFunc(w, r) 35 45 return 36 46 } 37 47 ··· 51 41 expiry, err := time.Parse(time.RFC3339, expiryStr) 52 42 if err != nil { 53 43 log.Println("invalid expiry time", err) 54 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 44 + redirectFunc(w, r) 55 45 return 56 46 } 57 47 pdsUrl, ok1 := session.Values[appview.SessionPds].(string) ··· 60 50 61 51 if !ok1 || !ok2 || !ok3 { 62 52 log.Println("invalid expiry time", err) 63 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 53 + redirectFunc(w, r) 64 54 return 65 55 } 66 56 ··· 78 68 atSession, err := comatproto.ServerRefreshSession(r.Context(), &client) 79 69 if err != nil { 80 70 log.Println("failed to refresh session", err) 81 - http.Redirect(w, r, "/login", http.StatusTemporaryRedirect) 71 + redirectFunc(w, r) 82 72 return 83 73 } 84 74