Skip to content

Commit 00d36dc

Browse files
author
glyphack
committed
use http header for authentication
1 parent d9db508 commit 00d36dc

File tree

1 file changed

+4
-5
lines changed

1 file changed

+4
-5
lines changed

README.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -766,16 +766,16 @@ type contextKey struct {
766766
func Middleware() func(http.Handler) http.Handler {
767767
return func(next http.Handler) http.Handler {
768768
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
769-
c, err := r.Cookie("token")
769+
header := r.Header.Get("Authorization")
770770

771771
// Allow unauthenticated users in
772-
if err != nil || c == nil {
772+
if header == "" {
773773
next.ServeHTTP(w, r)
774774
return
775775
}
776776

777777
//validate jwt token
778-
tokenStr := c.Value
778+
tokenStr := header
779779
username, err := jwt.ParseToken(tokenStr)
780780
if err != nil {
781781
http.Error(w, "Invalid token", http.StatusForbidden)
@@ -790,9 +790,8 @@ func Middleware() func(http.Handler) http.Handler {
790790
return
791791
}
792792
user.ID = strconv.Itoa(id)
793-
794793
// put it in context
795-
ctx := context.WithValue(r.Context(), userCtxKey, user)
794+
ctx := context.WithValue(r.Context(), userCtxKey, &user)
796795

797796
// and call the next with our new context
798797
r = r.WithContext(ctx)

0 commit comments

Comments
 (0)