From b3362d4a664d975997162d4bc3a9f888de687dd7 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Wed, 6 Nov 2024 19:48:02 +0200 Subject: [PATCH] Change user handlers to set cookies on registration and log in --- handlers/user.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/handlers/user.go b/handlers/user.go index 70e8c81..1cb3822 100644 --- a/handlers/user.go +++ b/handlers/user.go @@ -3,6 +3,7 @@ package handlers import ( "log" + "git.qowevisa.me/Qowevisa/fin-check-api/consts" "git.qowevisa.me/Qowevisa/fin-check-api/db" "git.qowevisa.me/Qowevisa/fin-check-api/tokens" "git.qowevisa.me/Qowevisa/fin-check-api/types" @@ -47,9 +48,11 @@ func UserRegister(c *gin.Context) { } token1 = token } + c.SetCookie(consts.COOKIE_SESSION, token1.Val, 3600, "/", "localhost", false, true) acc := types.Account{ - ID: dbUser.ID, - Token: token1.Val, + ID: dbUser.ID, + Token: token1.Val, + Username: dbUser.Username, } c.JSON(200, acc) } @@ -82,7 +85,7 @@ func UserLogin(c *gin.Context) { return } if foundUser.ID == 0 { - c.JSON(500, types.ErrorResponse{Message: "Credentials are incorrect"}) + c.JSON(400, types.ErrorResponse{Message: "Credentials are incorrect"}) return } var token1 *tokens.Token @@ -97,9 +100,13 @@ func UserLogin(c *gin.Context) { } token1 = token } + c.SetCookie(consts.COOKIE_SESSION, token1.Val, 3600, "/", "localhost", false, true) acc := types.Account{ - ID: foundUser.ID, - Token: token1.Val, + ID: foundUser.ID, + Token: token1.Val, + Username: dbUser.Username, } c.JSON(200, acc) } + +func isSessionTokenForUserInvalid(userID uint) {}