diff options
Diffstat (limited to 'backend/controllers/homeController.go')
| -rw-r--r-- | backend/controllers/homeController.go | 57 |
1 files changed, 0 insertions, 57 deletions
diff --git a/backend/controllers/homeController.go b/backend/controllers/homeController.go index 42dfaa7..8b52ff3 100644 --- a/backend/controllers/homeController.go +++ b/backend/controllers/homeController.go | |||
| @@ -2,14 +2,10 @@ package controllers | |||
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "net/http" | 4 | "net/http" |
| 5 | "os" | ||
| 6 | "time" | ||
| 7 | 5 | ||
| 8 | "github.com/gin-gonic/gin" | 6 | "github.com/gin-gonic/gin" |
| 9 | "github.com/golang-jwt/jwt/v4" | ||
| 10 | "github.com/pektezol/leastportals/backend/database" | 7 | "github.com/pektezol/leastportals/backend/database" |
| 11 | "github.com/pektezol/leastportals/backend/models" | 8 | "github.com/pektezol/leastportals/backend/models" |
| 12 | "github.com/solovev/steam_go" | ||
| 13 | ) | 9 | ) |
| 14 | 10 | ||
| 15 | func Home(c *gin.Context) { | 11 | func Home(c *gin.Context) { |
| @@ -23,59 +19,6 @@ func Home(c *gin.Context) { | |||
| 23 | } | 19 | } |
| 24 | } | 20 | } |
| 25 | 21 | ||
| 26 | func Login(c *gin.Context) { | ||
| 27 | openID := steam_go.NewOpenId(c.Request) | ||
| 28 | switch openID.Mode() { | ||
| 29 | case "": | ||
| 30 | c.Redirect(http.StatusMovedPermanently, openID.AuthUrl()) | ||
| 31 | case "cancel": | ||
| 32 | c.Redirect(http.StatusMovedPermanently, "/") | ||
| 33 | default: | ||
| 34 | steamID, err := openID.ValidateAndGetId() | ||
| 35 | if err != nil { | ||
| 36 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 37 | return | ||
| 38 | } | ||
| 39 | // Create user if new | ||
| 40 | var checkSteamID int64 | ||
| 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.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 44 | return | ||
| 45 | } | ||
| 46 | // User does not exist | ||
| 47 | if checkSteamID == 0 { | ||
| 48 | user, err := steam_go.GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | ||
| 49 | if err != nil { | ||
| 50 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 51 | return | ||
| 52 | } | ||
| 53 | // Insert new user to database | ||
| 54 | database.DB.Exec(`INSERT INTO users (steam_id, username, avatar_link, country_code) | ||
| 55 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) | ||
| 56 | } | ||
| 57 | // Generate JWT token | ||
| 58 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
| 59 | "sub": steamID, | ||
| 60 | "exp": time.Now().Add(time.Hour * 24 * 365).Unix(), | ||
| 61 | }) | ||
| 62 | // Sign and get the complete encoded token as a string using the secret | ||
| 63 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | ||
| 64 | if err != nil { | ||
| 65 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) | ||
| 66 | return | ||
| 67 | } | ||
| 68 | c.JSON(http.StatusOK, models.Response{ | ||
| 69 | Success: true, | ||
| 70 | Message: "Successfully generated token.", | ||
| 71 | Data: models.LoginResponse{ | ||
| 72 | Token: tokenString, | ||
| 73 | }, | ||
| 74 | }) | ||
| 75 | return | ||
| 76 | } | ||
| 77 | } | ||
| 78 | |||
| 79 | func Rankings(c *gin.Context) { | 22 | func Rankings(c *gin.Context) { |
| 80 | rows, err := database.DB.Query(`SELECT steam_id, username FROM users;`) | 23 | rows, err := database.DB.Query(`SELECT steam_id, username FROM users;`) |
| 81 | if err != nil { | 24 | if err != nil { |