mx := mux.NewRouter()
mx.Use(CorsHandler)
sch := mx.NewRoute().Subrouter()
sch.Use(middleware.ValidateSchoolToken)
teacher := mx.NewRoute().Subrouter()
teacher.Use(middleware.ValidateToken)
CorsHandler is not used when the code runs
mx := mux.NewRouter()
mx.Use(CorsHandler)
sch := mx.NewRoute().Subrouter()
sch.Use(middleware.ValidateSchoolToken)
teacher := mx.NewRoute().Subrouter()
teacher.Use(middleware.ValidateToken)
CorsHandler is not used when the code runs
Have you added http.MethodOptions
to your Methods call? I found this sample in the documentation:
func main() {
r := mux.NewRouter()
// IMPORTANT: you must specify an OPTIONS method matcher for the middleware to set CORS headers
r.HandleFunc("/foo", fooHandler).Methods(http.MethodGet, http.MethodPut, http.MethodPatch, http.MethodOptions)
r.Use(mux.CORSMethodMiddleware(r))
http.ListenAndServe(":8080", r)
}
Full docs: https://pkg.go.dev/github.com/gorilla/mux#readme-handling-cors-requests