-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathmiddleware_auth.go
39 lines (32 loc) · 1.03 KB
/
middleware_auth.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 gotuna
import (
"net/http"
)
// Authenticate middleware will redirect all guests to the destination.
// This is used to guard user-only routes and to force guests to login.
func (app App) Authenticate(destination string) MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if app.Session.IsGuest(r) {
http.Redirect(w, r, destination, http.StatusFound)
return
}
next.ServeHTTP(w, r)
})
}
}
// RedirectIfAuthenticated middleware will redirect authenticated
// users to the destination.
// This is used to deflect logged in users from guest-only pages like login
// or register page back to the app.
func (app App) RedirectIfAuthenticated(destination string) MiddlewareFunc {
return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if !app.Session.IsGuest(r) {
http.Redirect(w, r, destination, http.StatusFound)
return
}
next.ServeHTTP(w, r)
})
}
}