/
acl.go
39 lines (31 loc) · 1 KB
/
acl.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Package acl provides http.Handlers to prevent access to pages for
// authenticated users and for non-authenticated users.
package acl
import (
"net/http"
"github.com/blue-jay/blueprint/lib/flight"
)
// DisallowAuth does not allow authenticated users to access the page.
func DisallowAuth(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
// If user is authenticated, don't allow them to access the page
if c.Sess.Values["id"] != nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
h.ServeHTTP(w, r)
})
}
// DisallowAnon does not allow anonymous users to access the page.
func DisallowAnon(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c := flight.Context(w, r)
// If user is not authenticated, don't allow them to access the page
if c.Sess.Values["id"] == nil {
http.Redirect(w, r, "/", http.StatusFound)
return
}
h.ServeHTTP(w, r)
})
}