From 3104cc8f7a5761389af82b59677d16eb042ceda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?BENEDEK=20L=C3=A1szl=C3=B3?= Date: Sun, 1 Jun 2025 17:21:05 +0200 Subject: [PATCH] Rename UserController to AuthController --- api/auth.go | 12 ++++++------ controller/{UserController.go => AuthController.go} | 10 +++++----- controller/Factory.go | 8 ++++---- 3 files changed, 15 insertions(+), 15 deletions(-) rename controller/{UserController.go => AuthController.go} (87%) diff --git a/api/auth.go b/api/auth.go index 2f35f37..c97a0e3 100644 --- a/api/auth.go +++ b/api/auth.go @@ -38,7 +38,7 @@ func register(c *gin.Context) { }) return } - userController, err := controller.MakeUserController() + AuthController, err := controller.MakeAuthController() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ @@ -47,7 +47,7 @@ func register(c *gin.Context) { return } - err = userController.Register(transaction.Username, transaction.Password, transaction.RepeatPassword) + err = AuthController.Register(transaction.Username, transaction.Password, transaction.RepeatPassword) if err != nil { c.JSON(http.StatusBadRequest, gin.H{ @@ -74,7 +74,7 @@ func login(c *gin.Context) { return } - userController, err := controller.MakeUserController() + AuthController, err := controller.MakeAuthController() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), @@ -82,7 +82,7 @@ func login(c *gin.Context) { return } - token, ok, err := userController.Login(transaction.Username, transaction.Password) + token, ok, err := AuthController.Login(transaction.Username, transaction.Password) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), @@ -105,7 +105,7 @@ func login(c *gin.Context) { } func logout(c *gin.Context) { - userController, err := controller.MakeUserController() + AuthController, err := controller.MakeAuthController() if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), @@ -114,7 +114,7 @@ func logout(c *gin.Context) { } token, _ := c.Get(SESSION_COOKIE) // must exist after isLoggedIn - err = userController.Logout(token.(string)) + err = AuthController.Logout(token.(string)) if err != nil { c.JSON(http.StatusInternalServerError, gin.H{ "error": err.Error(), diff --git a/controller/UserController.go b/controller/AuthController.go similarity index 87% rename from controller/UserController.go rename to controller/AuthController.go index 2f8adfc..98b4a3f 100644 --- a/controller/UserController.go +++ b/controller/AuthController.go @@ -10,7 +10,7 @@ import ( "golang.org/x/crypto/bcrypt" ) -type UserController struct { +type AuthController struct { userDAO dao.IUserDAO sessionDAO dao.ISessionDAO } @@ -22,7 +22,7 @@ const ( TOKEN_LENGTH int = 32 ) -func (c *UserController) init() error { +func (c *AuthController) init() error { userDAO, err := dao.MakeUserDAO() c.userDAO = userDAO if err != nil { @@ -38,7 +38,7 @@ func (c *UserController) init() error { return nil } -func (c UserController) Register(username string, password string, repeatPassword string) error { +func (c AuthController) Register(username string, password string, repeatPassword string) error { if len(username) < MIN_USERNAME_LENGTH { return errors.New("username too short") } @@ -72,7 +72,7 @@ func generateToken(length int) (string, error) { return base64.URLEncoding.EncodeToString(b), nil } -func (c UserController) Login(username string, password string) (string, bool, error) { +func (c AuthController) Login(username string, password string) (string, bool, error) { user, err := c.userDAO.Read(model.User{Username: username}) if err != nil { return "", false, err @@ -95,6 +95,6 @@ func (c UserController) Login(username string, password string) (string, bool, e return token, true, nil } -func (c UserController) Logout(token string) error { +func (c AuthController) Logout(token string) error { return c.sessionDAO.Delete(token) } diff --git a/controller/Factory.go b/controller/Factory.go index 21c7dad..e247102 100644 --- a/controller/Factory.go +++ b/controller/Factory.go @@ -1,7 +1,7 @@ package controller -func MakeUserController() (UserController, error) { - userController := &UserController{} - err := userController.init() - return *userController, err +func MakeAuthController() (AuthController, error) { + AuthController := &AuthController{} + err := AuthController.init() + return *AuthController, err }