diff options
| author | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-06 23:55:12 +0300 |
|---|---|---|
| committer | Arda Serdar Pektezol <1669855+pektezol@users.noreply.github.com> | 2023-01-06 23:55:12 +0300 |
| commit | 4820c7696db3c54959258b1a5b00c77e1246cbd7 (patch) | |
| tree | 179edfca03618395939df1c3afccf2c90c64d424 /backend/controllers/controllers.go | |
| parent | (#20) successful integration of demo upload to drive (diff) | |
| download | lphub-4820c7696db3c54959258b1a5b00c77e1246cbd7.tar.gz lphub-4820c7696db3c54959258b1a5b00c77e1246cbd7.tar.bz2 lphub-4820c7696db3c54959258b1a5b00c77e1246cbd7.zip | |
what the fuck is this mess (#21)
Diffstat (limited to 'backend/controllers/controllers.go')
| -rw-r--r-- | backend/controllers/controllers.go | 88 |
1 files changed, 0 insertions, 88 deletions
diff --git a/backend/controllers/controllers.go b/backend/controllers/controllers.go deleted file mode 100644 index 5237ccd..0000000 --- a/backend/controllers/controllers.go +++ /dev/null | |||
| @@ -1,88 +0,0 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "log" | ||
| 5 | "net/http" | ||
| 6 | "os" | ||
| 7 | "time" | ||
| 8 | |||
| 9 | "github.com/gin-gonic/gin" | ||
| 10 | "github.com/golang-jwt/jwt/v4" | ||
| 11 | "github.com/pektezol/leastportals/backend/database" | ||
| 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 | http.Error(c.Writer, err.Error(), http.StatusInternalServerError) | ||
| 37 | } | ||
| 38 | // Create user if new | ||
| 39 | var checkSteamID int64 | ||
| 40 | database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) | ||
| 41 | // User does not exist | ||
| 42 | if checkSteamID == 0 { | ||
| 43 | user, err := steam_go.GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | ||
| 44 | if err != nil { | ||
| 45 | log.Panic(err) | ||
| 46 | } | ||
| 47 | // 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) | ||
| 49 | VALUES ($1, $2, $3, $4, $5, $6, $7)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode, time.Now().UTC(), time.Now().UTC(), 0) | ||
| 50 | } | ||
| 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 | ||
| 54 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
| 55 | "sub": steamID, | ||
| 56 | "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), | ||
| 57 | }) | ||
| 58 | // Sign and get the complete encoded token as a string using the secret | ||
| 59 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | ||
| 60 | if err != nil { | ||
| 61 | c.JSON(http.StatusBadRequest, gin.H{ | ||
| 62 | "error": "failed to create token", | ||
| 63 | }) | ||
| 64 | return | ||
| 65 | } | ||
| 66 | // Create auth cookie | ||
| 67 | c.SetSameSite(http.SameSiteLaxMode) | ||
| 68 | c.SetCookie("auth", tokenString, 3600*24*30, "/", "", true, true) | ||
| 69 | c.Redirect(http.StatusMovedPermanently, "/") | ||
| 70 | } | ||
| 71 | } | ||
| 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 | }) | ||
| 87 | } | ||
| 88 | } | ||