summaryrefslogtreecommitdiff
path: root/middleware/auth.go
blob: a42d59c59ea8ef11c6cd912a842419f2fd55d6d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
package middleware

import (
	"net/http"
	"net/url"

	"wyo.town/netzah/session"
)


func RequireAuth(next http.Handler) http.Handler {
	return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
		if !session.Manager.Exists(r.Context(), "user") {
			nextURL := url.QueryEscape(r.URL.RequestURI())
			http.Redirect(w, r, "/login?next="+nextURL, http.StatusFound)
			return
		}
		next.ServeHTTP(w, r)
	})
}