From f3453d12fd6ba7b16adad3712ae099216e6f470d Mon Sep 17 00:00:00 2001 From: Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> Date: Tue, 2 May 2023 22:53:02 +0300 Subject: feat: set cookie and redirect after login --- backend/controllers/loginController.go | 18 ++++++++++-------- main.go | 10 +++------- 2 files changed, 13 insertions(+), 15 deletions(-) diff --git a/backend/controllers/loginController.go b/backend/controllers/loginController.go index 4de4c00..c533757 100644 --- a/backend/controllers/loginController.go +++ b/backend/controllers/loginController.go @@ -62,7 +62,7 @@ func Login(c *gin.Context) { // Generate JWT token token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ "sub": steamID, - "exp": time.Now().Add(time.Hour * 24 * 365).Unix(), + "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), }) // Sign and get the complete encoded token as a string using the secret tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) @@ -70,13 +70,15 @@ func Login(c *gin.Context) { c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) return } - c.JSON(http.StatusOK, models.Response{ - Success: true, - Message: "Successfully generated token.", - Data: models.LoginResponse{ - Token: tokenString, - }, - }) + c.SetCookie("token", tokenString, 3600*24*30, "/", "", true, true) + c.Redirect(http.StatusTemporaryRedirect, "/") + // c.JSON(http.StatusOK, models.Response{ + // Success: true, + // Message: "Successfully generated token.", + // Data: models.LoginResponse{ + // Token: tokenString, + // }, + // }) return } } diff --git a/main.go b/main.go index 915955a..991f3c0 100644 --- a/main.go +++ b/main.go @@ -5,7 +5,6 @@ import ( "log" "os" - "github.com/gin-gonic/contrib/cors" "github.com/gin-gonic/gin" "github.com/joho/godotenv" "github.com/pektezol/leastportals/backend/database" @@ -20,8 +19,8 @@ import ( // @license.name GNU General Public License, Version 2 // @license.url https://www.gnu.org/licenses/old-licenses/gpl-2.0.html -// @host lp.ardapektezol.com/api -// @BasePath /v1 +// @host lp.ardapektezol.com/api +// @BasePath /v1 func main() { if os.Getenv("ENV") == "PROD" { gin.SetMode(gin.ReleaseMode) @@ -31,12 +30,9 @@ func main() { log.Fatal("Error loading .env file") } router := gin.Default() - router.Use(cors.New(cors.Config{ - AllowedOrigins: []string{"https://steamcommunity.com"}, - })) database.ConnectDB() // For frontend static serving - only for local debug - // router.Use(static.Serve("/", static.LocalFile("./frontend/dist", true))) + // router.Use(static.Serve("/", static.LocalFile("./frontend/build", true))) routes.InitRoutes(router) router.Run(fmt.Sprintf(":%s", os.Getenv("PORT"))) } -- cgit v1.2.3