From a392dffba053a3b0ded0087ec5ef122e588d3db3 Mon Sep 17 00:00:00 2001 From: kalipso Date: Thu, 25 Jun 2026 12:39:58 +0200 Subject: [PATCH] chore: user handle auth --- controllers/userController.go | 50 +++++++---------------------------- 1 file changed, 9 insertions(+), 41 deletions(-) diff --git a/controllers/userController.go b/controllers/userController.go index 5130a34..a359e2c 100644 --- a/controllers/userController.go +++ b/controllers/userController.go @@ -18,7 +18,7 @@ func NewUserController() UserController { func CreateSessionData(c *gin.Context, extra any) gin.H { user, exists := c.Get("user") - userImpl, _ := user.(openapi.UserDB) + userImpl, _ := user.(openapi.UserResponse) return gin.H{ "loggedIn": exists, @@ -32,39 +32,6 @@ func (rc *UserController) Logout(c *gin.Context) { c.HTML(http.StatusOK, "index.html", gin.H{}) } -//func (uc *UserController) Register(c *gin.Context) { -// //Get the email/passwd off req body -// var body struct { -// Name string -// Email string -// Password string -// } -// -// err := c.Bind(&body) -// -// if err != nil { -// c.JSON(http.StatusBadRequest, gin.H{ -// "error": "Failed to read body", -// }) -// -// return -// } -// -// _, err = services.Users.Register(body.Name, body.Email, body.Password, false) -// -// if err != nil { -// fmt.Println("Error: ", err) -// c.JSON(http.StatusBadRequest, gin.H{ -// "error": "Failed to create user", -// }) -// -// return -// } -// -// //respond -// c.JSON(http.StatusOK, gin.H{}) -//} - func (uc *UserController) Login(c *gin.Context) { //Get the email/passwd off req body var body struct { @@ -95,33 +62,34 @@ func (uc *UserController) Login(c *gin.Context) { // send it back c.SetSameSite(http.SameSiteLaxMode) - c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*24, "", "", false, true) + c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*1, "", "", false, true) c.JSON(http.StatusOK, gin.H{}) } func (rc *UserController) LoginHandler(c *gin.Context) { - email := c.PostForm("username") + username := c.PostForm("username") password := c.PostForm("password") - tokenString, err := services.Users.Login(email, password) + tokenString, err := services.Users.Login(username, password) if err != nil { data := gin.H{ - "error": "Login Failed. Wrong Email or Password!", + "error": "Login Failed. Wrong username or Password!", } c.HTML(http.StatusOK, "login.html", data) return } + c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*24, "", "", false, true) + //set this so that CreateSessionData works //otherwise header would not be generated correctly - //user, _ := repositories.Users.GetByEmail(email) - //c.Set("user", user) + user, _ := services.Users.GetByName(c, username) + c.Set("user", user) // send it back //c.SetSameSite(http.SameSiteLaxMode) - c.SetCookie("Authorization", tokenString.GetAccessToken(), 3600*24, "", "", false, true) c.HTML(http.StatusOK, "login.html", CreateSessionData(c, gin.H{})) }