diff options
Diffstat (limited to '')
| -rw-r--r-- | backend/handlers/login.go (renamed from backend/controllers/loginController.go) | 34 |
1 files changed, 19 insertions, 15 deletions
diff --git a/backend/controllers/loginController.go b/backend/handlers/login.go index e907b22..85ffd63 100644 --- a/backend/controllers/loginController.go +++ b/backend/handlers/login.go | |||
| @@ -1,9 +1,9 @@ | |||
| 1 | package controllers | 1 | package handlers |
| 2 | 2 | ||
| 3 | import ( | 3 | import ( |
| 4 | "encoding/json" | 4 | "encoding/json" |
| 5 | "fmt" | 5 | "fmt" |
| 6 | "io/ioutil" | 6 | "io" |
| 7 | "net/http" | 7 | "net/http" |
| 8 | "os" | 8 | "os" |
| 9 | "time" | 9 | "time" |
| @@ -15,13 +15,17 @@ import ( | |||
| 15 | "github.com/solovev/steam_go" | 15 | "github.com/solovev/steam_go" |
| 16 | ) | 16 | ) |
| 17 | 17 | ||
| 18 | type LoginResponse struct { | ||
| 19 | Token string `json:"token"` | ||
| 20 | } | ||
| 21 | |||
| 18 | // Login | 22 | // Login |
| 19 | // | 23 | // |
| 20 | // @Description Get (redirect) login page for Steam auth. | 24 | // @Description Get (redirect) login page for Steam auth. |
| 21 | // @Tags login | 25 | // @Tags login |
| 22 | // @Accept json | 26 | // @Accept json |
| 23 | // @Produce json | 27 | // @Produce json |
| 24 | // @Success 200 {object} models.Response{data=models.LoginResponse} | 28 | // @Success 200 {object} models.Response{data=LoginResponse} |
| 25 | // @Failure 400 {object} models.Response | 29 | // @Failure 400 {object} models.Response |
| 26 | // @Router /login [get] | 30 | // @Router /login [get] |
| 27 | func Login(c *gin.Context) { | 31 | func Login(c *gin.Context) { |
| @@ -34,20 +38,18 @@ func Login(c *gin.Context) { | |||
| 34 | default: | 38 | default: |
| 35 | steamID, err := openID.ValidateAndGetId() | 39 | steamID, err := openID.ValidateAndGetId() |
| 36 | if err != nil { | 40 | if err != nil { |
| 41 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailValidate) | ||
| 37 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 42 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 38 | return | 43 | return |
| 39 | } | 44 | } |
| 40 | // Create user if new | 45 | // Create user if new |
| 41 | var checkSteamID int64 | 46 | var checkSteamID int64 |
| 42 | err = database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) | 47 | database.DB.QueryRow("SELECT steam_id FROM users WHERE steam_id = $1", steamID).Scan(&checkSteamID) |
| 43 | // if err != nil { | ||
| 44 | // c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | ||
| 45 | // return | ||
| 46 | // } | ||
| 47 | // User does not exist | 48 | // User does not exist |
| 48 | if checkSteamID == 0 { | 49 | if checkSteamID == 0 { |
| 49 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) | 50 | user, err := GetPlayerSummaries(steamID, os.Getenv("API_KEY")) |
| 50 | if err != nil { | 51 | if err != nil { |
| 52 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailSummary) | ||
| 51 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) | 53 | c.JSON(http.StatusBadRequest, models.ErrorResponse(err.Error())) |
| 52 | return | 54 | return |
| 53 | } | 55 | } |
| @@ -60,7 +62,7 @@ func Login(c *gin.Context) { | |||
| 60 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) | 62 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) |
| 61 | } | 63 | } |
| 62 | moderator := false | 64 | moderator := false |
| 63 | rows, _ := database.DB.Query("SELECT title_name FROM titles WHERE user_id = $1", steamID) | 65 | rows, _ := database.DB.Query("SELECT title_name FROM titles t INNER JOIN user_titles ut ON t.id=ut.title_id WHERE ut.user_id = $1", steamID) |
| 64 | for rows.Next() { | 66 | for rows.Next() { |
| 65 | var title string | 67 | var title string |
| 66 | rows.Scan(&title) | 68 | rows.Scan(&title) |
| @@ -77,15 +79,17 @@ func Login(c *gin.Context) { | |||
| 77 | // Sign and get the complete encoded token as a string using the secret | 79 | // Sign and get the complete encoded token as a string using the secret |
| 78 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | 80 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) |
| 79 | if err != nil { | 81 | if err != nil { |
| 82 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginFailToken) | ||
| 80 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) | 83 | c.JSON(http.StatusBadRequest, models.ErrorResponse("Failed to generate token.")) |
| 81 | return | 84 | return |
| 82 | } | 85 | } |
| 83 | c.SetCookie("token", tokenString, 3600*24*30, "/", "", true, true) | 86 | c.SetCookie("token", tokenString, 3600*24*30, "/", "", true, true) |
| 87 | CreateLog(steamID, LogTypeUser, LogDescriptionUserLoginSuccess) | ||
| 84 | c.Redirect(http.StatusTemporaryRedirect, "/") | 88 | c.Redirect(http.StatusTemporaryRedirect, "/") |
| 85 | // c.JSON(http.StatusOK, models.Response{ | 89 | // c.JSON(http.StatusOK, models.Response{ |
| 86 | // Success: true, | 90 | // Success: true, |
| 87 | // Message: "Successfully generated token.", | 91 | // Message: "Successfully generated token.", |
| 88 | // Data: models.LoginResponse{ | 92 | // Data: LoginResponse{ |
| 89 | // Token: tokenString, | 93 | // Token: tokenString, |
| 90 | // }, | 94 | // }, |
| 91 | // }) | 95 | // }) |
| @@ -99,7 +103,7 @@ func Login(c *gin.Context) { | |||
| 99 | // @Tags auth | 103 | // @Tags auth |
| 100 | // @Produce json | 104 | // @Produce json |
| 101 | // | 105 | // |
| 102 | // @Success 200 {object} models.Response{data=models.LoginResponse} | 106 | // @Success 200 {object} models.Response{data=LoginResponse} |
| 103 | // @Failure 404 {object} models.Response | 107 | // @Failure 404 {object} models.Response |
| 104 | // @Router /token [get] | 108 | // @Router /token [get] |
| 105 | func GetCookie(c *gin.Context) { | 109 | func GetCookie(c *gin.Context) { |
| @@ -111,7 +115,7 @@ func GetCookie(c *gin.Context) { | |||
| 111 | c.JSON(http.StatusOK, models.Response{ | 115 | c.JSON(http.StatusOK, models.Response{ |
| 112 | Success: true, | 116 | Success: true, |
| 113 | Message: "Token cookie successfully retrieved.", | 117 | Message: "Token cookie successfully retrieved.", |
| 114 | Data: models.LoginResponse{ | 118 | Data: LoginResponse{ |
| 115 | Token: cookie, | 119 | Token: cookie, |
| 116 | }, | 120 | }, |
| 117 | }) | 121 | }) |
| @@ -123,7 +127,7 @@ func GetCookie(c *gin.Context) { | |||
| 123 | // @Tags auth | 127 | // @Tags auth |
| 124 | // @Produce json | 128 | // @Produce json |
| 125 | // | 129 | // |
| 126 | // @Success 200 {object} models.Response{data=models.LoginResponse} | 130 | // @Success 200 {object} models.Response{data=LoginResponse} |
| 127 | // @Failure 404 {object} models.Response | 131 | // @Failure 404 {object} models.Response |
| 128 | // @Router /token [delete] | 132 | // @Router /token [delete] |
| 129 | func DeleteCookie(c *gin.Context) { | 133 | func DeleteCookie(c *gin.Context) { |
| @@ -136,7 +140,7 @@ func DeleteCookie(c *gin.Context) { | |||
| 136 | c.JSON(http.StatusOK, models.Response{ | 140 | c.JSON(http.StatusOK, models.Response{ |
| 137 | Success: true, | 141 | Success: true, |
| 138 | Message: "Token cookie successfully deleted.", | 142 | Message: "Token cookie successfully deleted.", |
| 139 | Data: models.LoginResponse{ | 143 | Data: LoginResponse{ |
| 140 | Token: cookie, | 144 | Token: cookie, |
| 141 | }, | 145 | }, |
| 142 | }) | 146 | }) |
| @@ -148,7 +152,7 @@ func GetPlayerSummaries(steamId, apiKey string) (*models.PlayerSummaries, error) | |||
| 148 | if err != nil { | 152 | if err != nil { |
| 149 | return nil, err | 153 | return nil, err |
| 150 | } | 154 | } |
| 151 | body, err := ioutil.ReadAll(resp.Body) | 155 | body, err := io.ReadAll(resp.Body) |
| 152 | if err != nil { | 156 | if err != nil { |
| 153 | return nil, err | 157 | return nil, err |
| 154 | } | 158 | } |