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