@@ -75,28 +75,26 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Println(signed)
|
||||
}
|
||||
|
||||
func HandleMiddlewareRoute(pattern string,
|
||||
handler func(w http.ResponseWriter, r *http.Request),
|
||||
middleware func(http.Handler) http.Handler,
|
||||
mux *http.ServeMux,
|
||||
) {
|
||||
mux.HandleFunc(pattern, handler)
|
||||
http.Handle(pattern, middleware(mux))
|
||||
}
|
||||
|
||||
func AuthJWT(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Check if the Authorization header is provided
|
||||
fmt.Println("Coucou")
|
||||
authHeader := r.Header.Get("Authorization")
|
||||
if authHeader == "" {
|
||||
http.Error(w, "Missing Authorization header", http.StatusUnauthorized)
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Missing Authorization header",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Bearer token is expected, so split the header into "Bearer <token>"
|
||||
tokenString := strings.TrimPrefix(authHeader, "Bearer ")
|
||||
if tokenString == authHeader {
|
||||
http.Error(w, "Invalid Authorization header format", http.StatusUnauthorized)
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Invalid Authorization header format",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -110,7 +108,10 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
})
|
||||
|
||||
if err != nil || !token.Valid {
|
||||
http.Error(w, "Invalid token", http.StatusUnauthorized)
|
||||
JSONError{
|
||||
Status: Error,
|
||||
Message: "Invalid Token",
|
||||
}.Respond(w, http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user