-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathhttp.go
More file actions
35 lines (30 loc) · 971 Bytes
/
http.go
File metadata and controls
35 lines (30 loc) · 971 Bytes
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
// Copyright 2012 The GAEGo Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package user
import (
"net/http"
)
var LoginURL = "/login"
// LoginRequired is a wrapper for http.HandleFunc. If the requesting
// User is not logged in, they will be redirect to the login page.
func LoginRequired(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if id, _ := CurrentUserID(r); id == "" {
http.Redirect(w, r, LoginURL, http.StatusFound)
return
}
fn(w, r)
}
}
// AdminRequired is a wrapper for http.HandleFuc. If the requesting
// User is *not* an admin, they will redirect to the login page.
func AdminRequired(fn http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
if !CurrentUserHasRole(w, r, "admin") {
http.Redirect(w, r, LoginURL, http.StatusFound)
return
}
fn(w, r)
}
}