diff options
Diffstat (limited to '')
| -rw-r--r-- | backend/controllers/homeController.go (renamed from backend/controllers/controllers.go) | 53 |
1 files changed, 21 insertions, 32 deletions
diff --git a/backend/controllers/controllers.go b/backend/controllers/homeController.go index 5237ccd..8d81eef 100644 --- a/backend/controllers/controllers.go +++ b/backend/controllers/homeController.go | |||
| @@ -1,7 +1,6 @@ | |||
| 1 | package controllers | 1 | package controllers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "log" | ||
| 5 | "net/http" | 4 | "net/http" |
| 6 | "os" | 5 | "os" |
| 7 | "time" | 6 | "time" |
| @@ -9,6 +8,7 @@ import ( | |||
| 9 | "github.com/gin-gonic/gin" | 8 | "github.com/gin-gonic/gin" |
| 10 | "github.com/golang-jwt/jwt/v4" | 9 | "github.com/golang-jwt/jwt/v4" |
| 11 | "github.com/pektezol/leastportals/backend/database" | 10 | "github.com/pektezol/leastportals/backend/database" |
| 11 | "github.com/pektezol/leastportals/backend/models" | ||
| 12 | "github.com/solovev/steam_go" | 12 | "github.com/solovev/steam_go" |
| 13 | ) | 13 | ) |
| 14 | 14 | ||
| @@ -33,56 +33,45 @@ func Login(c *gin.Context) { | |||
| 33 | default: | 33 | default: |
| 34 | steamID, err := openID.ValidateAndGetId() | 34 | steamID, err := openID.ValidateAndGetId() |
| 35 | if err != nil { | 35 | if err != nil { |
| 36 | http.Error(c.Writer, err.Error(), http.StatusInternalServerError) | 36 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) |
| 37 | return | ||
| 37 | } | 38 | } |
| 38 | // Create user if new | 39 | // Create user if new |
| 39 | var checkSteamID int64 | 40 | var checkSteamID int64 |
| 40 | database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) | 41 | err = database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) |
| 42 | if err != nil { | ||
| 43 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) | ||
| 44 | return | ||
| 45 | } | ||
| 41 | // User does not exist | 46 | // User does not exist |
| 42 | if checkSteamID == 0 { | 47 | if checkSteamID == 0 { |
| 43 | user, err := steam_go.GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | 48 | user, err := steam_go.GetPlayerSummaries(steamID, os.Getenv("API_KEY")) |
| 44 | if err != nil { | 49 | if err != nil { |
| 45 | log.Panic(err) | 50 | c.JSON(http.StatusInternalServerError, models.ErrorResponse(err.Error())) |
| 51 | return | ||
| 46 | } | 52 | } |
| 47 | // Insert new user to database | 53 | // Insert new user to database |
| 48 | database.DB.Exec(`INSERT INTO users (steam_id, username, avatar_link, country_code, created_at, updated_at, user_type) | 54 | database.DB.Exec(`INSERT INTO users (steam_id, username, avatar_link, country_code) |
| 49 | VALUES ($1, $2, $3, $4, $5, $6, $7)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode, time.Now().UTC(), time.Now().UTC(), 0) | 55 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) |
| 50 | } | 56 | } |
| 51 | // Update updated_at | ||
| 52 | database.DB.Exec(`UPDATE users SET updated_at = $1 WHERE steam_id = $2`, time.Now().UTC(), steamID) | ||
| 53 | // Generate JWT token | 57 | // Generate JWT token |
| 54 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | 58 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
| 55 | "sub": steamID, | 59 | "sub": steamID, |
| 56 | "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), | 60 | "exp": time.Now().Add(time.Hour * 24 * 365).Unix(), |
| 57 | }) | 61 | }) |
| 58 | // Sign and get the complete encoded token as a string using the secret | 62 | // Sign and get the complete encoded token as a string using the secret |
| 59 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | 63 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) |
| 60 | if err != nil { | 64 | if err != nil { |
| 61 | c.JSON(http.StatusBadRequest, gin.H{ | 65 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) |
| 62 | "error": "failed to create token", | ||
| 63 | }) | ||
| 64 | return | 66 | return |
| 65 | } | 67 | } |
| 66 | // Create auth cookie | 68 | c.JSON(http.StatusOK, models.Response{ |
| 67 | c.SetSameSite(http.SameSiteLaxMode) | 69 | Success: true, |
| 68 | c.SetCookie("auth", tokenString, 3600*24*30, "/", "", true, true) | 70 | Message: "Successfully generated token.", |
| 69 | c.Redirect(http.StatusMovedPermanently, "/") | 71 | Data: models.LoginResponse{ |
| 70 | } | 72 | Token: tokenString, |
| 71 | } | 73 | }, |
| 72 | |||
| 73 | func Logout(c *gin.Context) { | ||
| 74 | // Check if user exists | ||
| 75 | _, exists := c.Get("user") | ||
| 76 | if !exists { | ||
| 77 | c.JSON(http.StatusBadRequest, gin.H{ | ||
| 78 | "error": "not logged in", | ||
| 79 | }) | ||
| 80 | } else { | ||
| 81 | // Set auth cookie to die | ||
| 82 | tokenString, _ := c.Cookie("auth") | ||
| 83 | c.SetCookie("auth", tokenString, -1, "/", "", true, true) | ||
| 84 | c.JSON(http.StatusOK, gin.H{ | ||
| 85 | "output": "logout success", | ||
| 86 | }) | 74 | }) |
| 75 | return | ||
| 87 | } | 76 | } |
| 88 | } | 77 | } |