From 1f8930fd282e415fb5a02b2a835e9483e3d909e4 Mon Sep 17 00:00:00 2001 From: Emyr298 Date: Sat, 29 Jun 2024 10:23:04 +0700 Subject: [PATCH] refactor: move users logic to integrations --- src/handlers/auth_handler.go | 48 ++++++----------------------- src/integrations/users/users_api.go | 44 ++++++++++++++++++++++++++ 2 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 src/integrations/users/users_api.go diff --git a/src/handlers/auth_handler.go b/src/handlers/auth_handler.go index 9454b12..c6330c0 100644 --- a/src/handlers/auth_handler.go +++ b/src/handlers/auth_handler.go @@ -1,14 +1,11 @@ package handlers import ( - "encoding/json" "errors" - "io" "log" - "net/http" - "os" "pixeltactics.com/match/src/exceptions" + integration_users "pixeltactics.com/match/src/integrations/users" "pixeltactics.com/match/src/utils" ws_types "pixeltactics.com/match/src/websocket/types" ) @@ -100,46 +97,21 @@ func (handler *AuthHandler) sendAuthRequest(playerToken string, res *ws_types.Re } } - host := os.Getenv("USER_MICROSERVICE_URL") - client := &http.Client{} - req, err := http.NewRequest("GET", host+"/auth/current", nil) + playerId, err := integration_users.GetUsernameFromToken(playerToken) if err != nil { - log.Println("ERROR REQUEST MAKE") sendError() + return } - req.Header.Set("Authorization", "Bearer "+playerToken) - resp, err := client.Do(req) - if err != nil { - log.Println("ERROR REQUEST SEND") - sendError() - } - - if resp.StatusCode == 200 { - jsonBytes, err := io.ReadAll(resp.Body) - if err != nil { - sendError() - } - - var body map[string]string - json.Unmarshal(jsonBytes, &body) - - playerId, ok := body["username"] - if !ok { - sendError() - } - handler.successResponses <- &ws_types.Interaction{ - Request: &ws_types.Request{ - Message: &ws_types.Message{ - Body: map[string]interface{}{ - "playerId": playerId, - }, + handler.successResponses <- &ws_types.Interaction{ + Request: &ws_types.Request{ + Message: &ws_types.Message{ + Body: map[string]interface{}{ + "playerId": playerId, }, }, - Response: res, - } - } else { - sendError() + }, + Response: res, } }() } diff --git a/src/integrations/users/users_api.go b/src/integrations/users/users_api.go new file mode 100644 index 0000000..4df9ca7 --- /dev/null +++ b/src/integrations/users/users_api.go @@ -0,0 +1,44 @@ +package integration_users + +import ( + "encoding/json" + "errors" + "io" + "net/http" + "os" +) + +func GetUsernameFromToken(playerToken string) (string, error) { + host := os.Getenv("USER_MICROSERVICE_URL") + + client := &http.Client{} + req, err := http.NewRequest("GET", host+"/auth/current", nil) + if err != nil { + return "", err + } + + req.Header.Set("Authorization", "Bearer "+playerToken) + resp, err := client.Do(req) + if err != nil { + return "", err + } + + if resp.StatusCode != 200 { + return "", errors.New("invalid token") + } + + jsonBytes, err := io.ReadAll(resp.Body) + if err != nil { + return "", err + } + + var body map[string]string + json.Unmarshal(jsonBytes, &body) + + playerId, ok := body["username"] + if !ok { + return "", errors.New("invalid json body") + } + + return playerId, nil +}