Middlewares
This commit is contained in:
@@ -10,12 +10,13 @@ import (
|
||||
"time"
|
||||
|
||||
core "fr.latosa-escrima/api/core"
|
||||
"fr.latosa-escrima/utils"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
)
|
||||
|
||||
var MySigningKey = []byte("COUCOU")
|
||||
|
||||
type LoginInformation struct {
|
||||
type LoginArgs struct {
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
}
|
||||
@@ -26,13 +27,13 @@ type Claims struct {
|
||||
}
|
||||
|
||||
func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
// if r.Method != http.MethodPost {
|
||||
// core.JSONError{
|
||||
// Status: core.Error,
|
||||
// Message: "Method is not allowed",
|
||||
// }.Respond(w, http.StatusMethodNotAllowed)
|
||||
// return
|
||||
// }
|
||||
|
||||
if r.Body == nil {
|
||||
core.JSONError{
|
||||
@@ -50,7 +51,7 @@ func HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
}.Respond(w, http.StatusNoContent)
|
||||
return
|
||||
}
|
||||
var login LoginInformation
|
||||
var login LoginArgs
|
||||
err = json.Unmarshal(body, &login)
|
||||
if err != nil {
|
||||
core.JSONError{
|
||||
@@ -151,3 +152,25 @@ func AuthJWT(next http.Handler) http.Handler {
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
// @param methods string -> HttpMethods separated by commas.
|
||||
func Methods(methods string) core.Middleware {
|
||||
_methods := strings.Split(strings.ToUpper(methods), ",")
|
||||
middleware := func(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if len(methods) > 0 {
|
||||
if !utils.Contains(_methods, r.Method) {
|
||||
core.JSONError{
|
||||
Status: core.Error,
|
||||
Message: "Method is not allowed.",
|
||||
}.Respond(w, http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
return middleware
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user