From dbad5ed64b88394591c81222a9ab4acb3dbea24f Mon Sep 17 00:00:00 2001 From: Chris Cromer Date: Wed, 1 Feb 2023 16:23:25 -0300 Subject: [PATCH] separate the auth routes from the user routes --- backend/controllers/auth.go | 64 +++++++++++++++++++++++++++++++++++++ backend/controllers/user.go | 53 ------------------------------ backend/routes/auth.go | 13 ++++++++ backend/routes/router.go | 1 + backend/routes/user.go | 3 -- 5 files changed, 78 insertions(+), 56 deletions(-) create mode 100644 backend/controllers/auth.go create mode 100644 backend/routes/auth.go diff --git a/backend/controllers/auth.go b/backend/controllers/auth.go new file mode 100644 index 0000000..a9a3ec2 --- /dev/null +++ b/backend/controllers/auth.go @@ -0,0 +1,64 @@ +package controllers + +import ( + "encoding/json" + "errors" + "net/http" + + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/database" + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/models" + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/utils" + "github.com/julienschmidt/httprouter" +) + +func Login(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + gdb := database.Connect() + defer database.Close(gdb) + + var receivedUser models.User + var user models.User + + decoder := json.NewDecoder(request.Body) + + err := decoder.Decode(&receivedUser) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + result := gdb.Model(models.User{}).Where(&models.User{Username: receivedUser.Username}).Find(&user) + if result.Error != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) + return + } else if result.RowsAffected == 0 { + utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error()) + return + } + + err = user.CheckPassword(receivedUser.Password) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error()) + return + } + + type Token struct { + Token string `json:"token"` + } + + tokenString, err := utils.GenerateJWT(user.Email, user.Username) + if err != nil { + utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) + return + } + + token := Token{Token: tokenString} + + writer.Header().Set("Content-Type", "application/json") + writer.WriteHeader(http.StatusOK) + json.NewEncoder(writer).Encode(token) +} + +func AuthenticateUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { + writer.Header().Set("Content-Type", "application/json") + writer.WriteHeader(http.StatusNoContent) +} diff --git a/backend/controllers/user.go b/backend/controllers/user.go index d0e9418..5fa0ab1 100644 --- a/backend/controllers/user.go +++ b/backend/controllers/user.go @@ -2,7 +2,6 @@ package controllers import ( "encoding/json" - "errors" "math" "net/http" "strconv" @@ -14,58 +13,6 @@ import ( "github.com/julienschmidt/httprouter" ) -func Login(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { - gdb := database.Connect() - defer database.Close(gdb) - - var receivedUser models.User - var user models.User - - decoder := json.NewDecoder(request.Body) - - err := decoder.Decode(&receivedUser) - if err != nil { - utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) - return - } - - result := gdb.Model(models.User{}).Where(&models.User{Username: receivedUser.Username}).Find(&user) - if result.Error != nil { - utils.JSONErrorOutput(writer, http.StatusBadRequest, result.Error.Error()) - return - } else if result.RowsAffected == 0 { - utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error()) - return - } - - err = user.CheckPassword(receivedUser.Password) - if err != nil { - utils.JSONErrorOutput(writer, http.StatusBadRequest, errors.New("incorrect user or password").Error()) - return - } - - type Token struct { - Token string `json:"token"` - } - - tokenString, err := utils.GenerateJWT(user.Email, user.Username) - if err != nil { - utils.JSONErrorOutput(writer, http.StatusBadRequest, err.Error()) - return - } - - token := Token{Token: tokenString} - - writer.Header().Set("Content-Type", "application/json") - writer.WriteHeader(http.StatusOK) - json.NewEncoder(writer).Encode(token) -} - -func AuthenticateUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { - writer.Header().Set("Content-Type", "application/json") - writer.WriteHeader(http.StatusNoContent) -} - func ListUser(writer http.ResponseWriter, request *http.Request, params httprouter.Params) { gdb := database.Connect() defer database.Close(gdb) diff --git a/backend/routes/auth.go b/backend/routes/auth.go new file mode 100644 index 0000000..47d627e --- /dev/null +++ b/backend/routes/auth.go @@ -0,0 +1,13 @@ +package routes + +import ( + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/controllers" + "git.cromer.cl/Proyecto-Titulo/alai-server/backend/middlewares" + + "github.com/julienschmidt/httprouter" +) + +func AuthRoutes(router *httprouter.Router) { + router.POST("/login", controllers.Login) + router.GET("/auth", middlewares.Authenticate(controllers.AuthenticateUser)) +} diff --git a/backend/routes/router.go b/backend/routes/router.go index 9d0261b..f06bddc 100644 --- a/backend/routes/router.go +++ b/backend/routes/router.go @@ -17,6 +17,7 @@ import ( func Initialize() *httprouter.Router { router := httprouter.New() router.GET("/", index) + AuthRoutes(router) FrameRoutes(router) GameRoutes(router) GodotVersionRoutes(router) diff --git a/backend/routes/user.go b/backend/routes/user.go index 11bea57..8376c18 100644 --- a/backend/routes/user.go +++ b/backend/routes/user.go @@ -8,9 +8,6 @@ import ( ) func UserRoutes(router *httprouter.Router) { - router.POST("/login", controllers.Login) - router.GET("/auth", middlewares.Authenticate(controllers.AuthenticateUser)) - router.GET("/user", middlewares.Authenticate(controllers.ListUser)) router.GET("/user/:id", middlewares.Authenticate(controllers.GetUser)) router.POST("/user", middlewares.Authenticate(controllers.CreateUser))