Added /users/me route, and handling auth in frontend
This commit is contained in:
@@ -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
33
backend/api/get_me.go
Normal 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)
|
||||
}
|
||||
@@ -17,6 +17,21 @@ import (
|
||||
func handler(w http.ResponseWriter, r *http.Request) {
|
||||
fmt.Fprintf(w, "<html><body><h1>Hello, World!</h1></body></html>")
|
||||
}
|
||||
func Cors(next http.Handler) http.Handler {
|
||||
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
// Allow all origins (can restrict to specific origins)
|
||||
w.Header().Set("Access-Control-Allow-Origin", "*")
|
||||
// Allow certain HTTP methods (you can customize these as needed)
|
||||
w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS, PATCH")
|
||||
// Allow certain headers (you can add more as needed)
|
||||
w.Header().Set("Access-Control-Allow-Headers", "Content-Type, Authorization")
|
||||
// Handle OPTIONS pre-flight request
|
||||
if r.Method == http.MethodOptions {
|
||||
return
|
||||
}
|
||||
next.ServeHTTP(w, r)
|
||||
})
|
||||
}
|
||||
|
||||
func main() {
|
||||
err := godotenv.Load()
|
||||
@@ -52,10 +67,13 @@ func main() {
|
||||
core.HandleRoutes(mux, map[string]core.Handler{
|
||||
"/": {
|
||||
Handler: handler,
|
||||
Middlewares: []core.Middleware{api.Methods("post")}},
|
||||
Middlewares: []core.Middleware{api.Methods("get")}},
|
||||
"/users/login": {
|
||||
Handler: api.HandleLogin,
|
||||
Middlewares: []core.Middleware{api.Methods("POST")}},
|
||||
"/users/me": {
|
||||
Handler: api.HandleGetMe,
|
||||
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
||||
"/users": {
|
||||
Handler: api.HandleGetUsers,
|
||||
Middlewares: []core.Middleware{api.Methods("GET"), api.AuthJWT}},
|
||||
@@ -80,7 +98,7 @@ func main() {
|
||||
})
|
||||
|
||||
fmt.Printf("Serving on port %s\n", port)
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%s", port), mux)
|
||||
err = http.ListenAndServe(fmt.Sprintf(":%s", port), Cors(mux))
|
||||
if err != nil {
|
||||
fmt.Printf("Error starting server: %s\n", err)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user