Handling properly now ?

This commit is contained in:
cdricms
2025-01-15 15:16:16 +01:00
parent 3ac25f2a83
commit ca313299cf
3 changed files with 85 additions and 13 deletions

View File

@@ -74,28 +74,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
}
@@ -109,7 +107,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
}