Added /users/me route, and handling auth in frontend

This commit is contained in:
cdricms
2025-01-17 15:37:01 +01:00
parent 5405cc50d9
commit eb9883a1c3
20 changed files with 453 additions and 68 deletions

View File

@@ -135,8 +135,10 @@ func AuthJWT(next http.Handler) http.Handler {
return
}
ctx := context.WithValue(r.Context(), "token", token)
// Call the next handler if the JWT is valid
next.ServeHTTP(w, r)
next.ServeHTTP(w, r.WithContext(ctx))
})
}

33
backend/api/get_me.go Normal file
View File

@@ -0,0 +1,33 @@
package api
import (
"net/http"
"fr.latosa-escrima/api/core"
"github.com/golang-jwt/jwt/v5"
)
func HandleGetMe(w http.ResponseWriter, r *http.Request) {
token, ok := r.Context().Value("token").(*jwt.Token)
if !ok {
core.JSONError{
Status: core.Error,
Message: "Couldn't retrieve your JWT.",
}.Respond(w, http.StatusInternalServerError)
return
}
claims, ok := token.Claims.(jwt.MapClaims)
if !ok {
core.JSONError{
Status: core.Error,
Message: "Invalid token claims.",
}.Respond(w, http.StatusInternalServerError)
return
}
uuid := claims["user_id"].(string)
r.SetPathValue("user_uuid", uuid)
HandleGetUser(w, r)
}