diff options
| author | Alexander Kavon <hawk@alexkavon.com> | 2023-11-30 05:28:59 -0500 |
|---|---|---|
| committer | Alexander Kavon <hawk@alexkavon.com> | 2023-11-30 05:28:59 -0500 |
| commit | ac7f557d6039ec10518e39af7259e5e97f59fd38 (patch) | |
| tree | 7a4fc997aeb759b681565ca326fe888ed899d003 | |
| parent | ec1f49a98765800481608394af3feb5a5221110c (diff) | |
properly check value in session middlewhere and reverse rules
| -rw-r--r-- | src/sessions/middleware.go | 18 |
1 files changed, 9 insertions, 9 deletions
diff --git a/src/sessions/middleware.go b/src/sessions/middleware.go index c1d0528..6bb3b15 100644 --- a/src/sessions/middleware.go +++ b/src/sessions/middleware.go @@ -27,23 +27,23 @@ func SetSession(next http.Handler) http.Handler { func GuestSession(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // if SessionKey does not exist then this is a valid guest request - if _, ok := r.Context().Value(SessionCtxKey("session")); !ok { - next.ServeHTTP(w, r) + // if SessionKey does exist then redirect to `/u/me` as this is an auth session + if v := r.Context().Value(SessionCtxKey("session")); v != nil { + http.Redirect(w, r, "/u/me", http.StatusSeeOther) } - // else redirect to `/u/me` as this is an auth session - http.Redirect(w, r, "/u/me", http.StatusSeeOther) + // else this is a valid guest request + next.ServeHTTP(w, r) }) } func AuthSession(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { - // if session key not exists, then this is a guest session request redirect to login page - if _, ok := r.Context().Value(SessionCtxKey("session")); !ok { + // if session key exists then this is a valid auth request + if v := r.Context().Value(SessionCtxKey("session")); v != nil { http.Redirect(w, r, "/login", http.StatusSeeOther) } - // else this is a valid auth session request - next.ServeHTTP(w, r) + // else this is a guest session request, redirect to login + http.Redirect(w, r, "/login", http.StatusSeeOther) }) } |
