diff --git a/backend/auth.go b/backend/auth.go index 9c55e6b..6f8cca1 100644 --- a/backend/auth.go +++ b/backend/auth.go @@ -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 " 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 } diff --git a/backend/main.go b/backend/main.go index fbd9c7d..d33bf7e 100644 --- a/backend/main.go +++ b/backend/main.go @@ -32,6 +32,14 @@ func (dsn *DSN) ToString() string { return fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable", dsn.User, dsn.Password, dsn.Hostname, dsn.Port, dsn.DBName) } +func er(w http.ResponseWriter, r *http.Request) { + _ = r + JSONError{ + Status: Error, + Message: "Nope", + }.Respond(w, http.StatusUnauthorized) +} + func handlerCreateUser(w http.ResponseWriter, r *http.Request) { if r.Method != http.MethodPost { @@ -83,6 +91,7 @@ func main() { "/": {handler, nil}, "/users/login": {HandleLogin, nil}, "/users/new": {handlerCreateUser, AuthJWT}, + "/error": {er, nil}, }) fmt.Printf("Serving on port %s\n", port) diff --git a/backend/router.go b/backend/router.go index aaf2133..31207ee 100644 --- a/backend/router.go +++ b/backend/router.go @@ -1,6 +1,68 @@ package main -import "net/http" +import ( + "encoding/json" + "net/http" +) + +type JSONStatus string + +const ( + Error JSONStatus = "Error" + Success JSONStatus = "Success" +) + +type JSONResponse interface { + ToJSON() ([]byte, error) + Respond(w http.ResponseWriter, code int) +} + +type JSONError struct { + Status JSONStatus `json:"status"` + Message string `json:"message"` +} + +type JSONSuccess struct { + Status JSONStatus `json:"status"` + Message string `json:"message"` + Data any `json:"data,omitempty"` +} + +func (r *JSONError) ToJSON() ([]byte, error) { + return json.Marshal(r) +} + +func (r *JSONSuccess) ToJSON() ([]byte, error) { + return json.Marshal(r) +} + +func defaultResponse(r JSONResponse, w http.ResponseWriter, code int) { + jsonData, err := r.ToJSON() + if err != nil { + http.Error(w, err.Error(), http.StatusNotAcceptable) + } + + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(code) + w.Write(jsonData) +} + +func (r JSONError) Respond(w http.ResponseWriter, code int) { + defaultResponse(&r, w, code) +} + +func (r JSONSuccess) Respond(w http.ResponseWriter, code int) { + defaultResponse(&r, w, code) +} + +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) + mux.Handle(pattern, middleware(http.HandlerFunc(handler))) +} type HandlerFunc func(w http.ResponseWriter, r *http.Request)