diff options
Diffstat (limited to 'backend/controllers/homeController.go')
| -rw-r--r-- | backend/controllers/homeController.go | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/backend/controllers/homeController.go b/backend/controllers/homeController.go new file mode 100644 index 0000000..8d81eef --- /dev/null +++ b/backend/controllers/homeController.go | |||
| @@ -0,0 +1,77 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "net/http" | ||
| 5 | "os" | ||
| 6 | "time" | ||
| 7 | |||
| 8 | "github.com/gin-gonic/gin" | ||
| 9 | "github.com/golang-jwt/jwt/v4" | ||
| 10 | "github.com/pektezol/leastportals/backend/database" | ||
| 11 | "github.com/pektezol/leastportals/backend/models" | ||
| 12 | "github.com/solovev/steam_go" | ||
| 13 | ) | ||
| 14 | |||
| 15 | func Home(c *gin.Context) { | ||
| 16 | user, exists := c.Get("user") | ||
| 17 | if !exists { | ||
| 18 | c.JSON(200, "no id, not auth") | ||
| 19 | } else { | ||
| 20 | c.JSON(200, gin.H{ | ||
| 21 | "output": user, | ||
| 22 | }) | ||
| 23 | } | ||
| 24 | } | ||
| 25 | |||
| 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.StatusInternalServerError, 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.StatusInternalServerError, 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.StatusInternalServerError, 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 | } | ||