blob: 0334e78799ce8e83b9487c584bfb240b73416e7b (
plain)
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
|
package middleware
import (
"encoding/json"
"net/http"
"github.com/boozec/rahanna/internal/api/auth"
)
func AuthMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
tokenString := r.Header.Get("Authorization")
payloadMap := map[string]string{"error": "unauthorized"}
payload, _ := json.Marshal(payloadMap)
if tokenString == "" {
w.Header().Set("Content-Type", "application/json")
w.WriteHeader(http.StatusUnauthorized)
w.Write([]byte(payload))
return
}
_, err := auth.ValidateJWT(tokenString)
if err != nil {
w.WriteHeader(http.StatusUnauthorized)
payload, _ := json.Marshal(payloadMap)
w.Write([]byte(payload))
return
}
next.ServeHTTP(w, r)
})
}
|