Skip to content

Commit 96ee0ce

Browse files
committed
auth middleware
1 parent f8b012d commit 96ee0ce

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

middlewares.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package access
2+
3+
import (
4+
"context"
5+
"net/http"
6+
)
7+
8+
func AuthMiddleware(next http.HandlerFunc) http.HandlerFunc {
9+
return func(w http.ResponseWriter, r *http.Request) {
10+
userID, authorized, err := IsAuthorized(w, r, nil)
11+
if err != nil {
12+
http.Error(w, err.Error(), http.StatusInternalServerError)
13+
return
14+
}
15+
16+
if !authorized {
17+
// Response already handled by IsAuthorized.
18+
return
19+
}
20+
21+
ctx := context.WithValue(r.Context(), "userID", userID)
22+
r = r.WithContext(ctx)
23+
next(w, r)
24+
}
25+
}

0 commit comments

Comments
 (0)