diff options
Diffstat (limited to 'backend/controllers/loginController.go')
| -rw-r--r-- | backend/controllers/loginController.go | 92 |
1 files changed, 92 insertions, 0 deletions
diff --git a/backend/controllers/loginController.go b/backend/controllers/loginController.go new file mode 100644 index 0000000..50189e8 --- /dev/null +++ b/backend/controllers/loginController.go | |||
| @@ -0,0 +1,92 @@ | |||
| 1 | package controllers | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "encoding/json" | ||
| 5 | "fmt" | ||
| 6 | "io/ioutil" | ||
| 7 | "net/http" | ||
| 8 | "os" | ||
| 9 | "time" | ||
| 10 | |||
| 11 | "github.com/gin-gonic/gin" | ||
| 12 | "github.com/golang-jwt/jwt/v4" | ||
| 13 | "github.com/pektezol/leastportals/backend/database" | ||
| 14 | "github.com/pektezol/leastportals/backend/models" | ||
| 15 | "github.com/solovev/steam_go" | ||
| 16 | ) | ||
| 17 | |||
| 18 | func Login(c *gin.Context) { | ||
| 19 | openID := steam_go.NewOpenId(c.Request) | ||
| 20 | switch openID.Mode() { | ||
| 21 | case "": | ||
| 22 | c.Redirect(http.StatusMovedPermanently, openID.AuthUrl()) | ||
| 23 | case "cancel": | ||
| 24 | c.Redirect(http.StatusMovedPermanently, "/") | ||
| 25 | default: | ||
| 26 | steamID, err := openID.ValidateAndGetId() | ||
| 27 | if err != nil { | ||
| 28 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 29 | return | ||
| 30 | } | ||
| 31 | // Create user if new | ||
| 32 | var checkSteamID int64 | ||
| 33 | err = database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) | ||
| 34 | if err != nil { | ||
| 35 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 36 | return | ||
| 37 | } | ||
| 38 | // User does not exist | ||
| 39 | if checkSteamID == 0 { | ||
| 40 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | ||
| 41 | if err != nil { | ||
| 42 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 43 | return | ||
| 44 | } | ||
| 45 | // Insert new user to database | ||
| 46 | database.DB.Exec(`INSERT INTO users (steam_id, username, avatar_link, country_code) | ||
| 47 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) | ||
| 48 | } | ||
| 49 | // Generate JWT token | ||
| 50 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | ||
| 51 | "sub": steamID, | ||
| 52 | "exp": time.Now().Add(time.Hour * 24 * 365).Unix(), | ||
| 53 | }) | ||
| 54 | // Sign and get the complete encoded token as a string using the secret | ||
| 55 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | ||
| 56 | if err != nil { | ||
| 57 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) | ||
| 58 | return | ||
| 59 | } | ||
| 60 | c.JSON(http.StatusOK, models.Response{ | ||
| 61 | Success: true, | ||
| 62 | Message: "Successfully generated token.", | ||
| 63 | Data: models.LoginResponse{ | ||
| 64 | Token: tokenString, | ||
| 65 | }, | ||
| 66 | }) | ||
| 67 | return | ||
| 68 | } | ||
| 69 | } | ||
| 70 | |||
| 71 | func GetPlayerSummaries(steamId, apiKey string) (*models.PlayerSummaries, error) { | ||
| 72 | url := fmt.Sprintf("http://api.steampowered.com/ISteamUser/GetPlayerSummaries/v2/?key=%s&steamids=%s", apiKey, steamId) | ||
| 73 | resp, err := http.Get(url) | ||
| 74 | if err != nil { | ||
| 75 | return nil, err | ||
| 76 | } | ||
| 77 | body, err := ioutil.ReadAll(resp.Body) | ||
| 78 | if err != nil { | ||
| 79 | return nil, err | ||
| 80 | } | ||
| 81 | |||
| 82 | type Result struct { | ||
| 83 | Response struct { | ||
| 84 | Players []models.PlayerSummaries `json:"players"` | ||
| 85 | } `json:"response"` | ||
| 86 | } | ||
| 87 | var data Result | ||
| 88 | if err := json.Unmarshal(body, &data); err != nil { | ||
| 89 | return nil, err | ||
| 90 | } | ||
| 91 | return &data.Response.Players[0], err | ||
| 92 | } | ||