diff options
| author | Nidboj132 <lol2s@vp.plm> | 2023-07-12 17:58:23 +0200 |
|---|---|---|
| committer | Nidboj132 <lol2s@vp.plm> | 2023-07-12 17:58:23 +0200 |
| commit | 781289455037431d8adbaa0b293b755c88169747 (patch) | |
| tree | 773824f97c3b21d353b9066afdbde30bee2da4c5 /backend/controllers/loginController.go | |
| parent | summary (diff) | |
| parent | fix: 0 score count / showcase not required (#47) (diff) | |
| download | lphub-781289455037431d8adbaa0b293b755c88169747.tar.gz lphub-781289455037431d8adbaa0b293b755c88169747.tar.bz2 lphub-781289455037431d8adbaa0b293b755c88169747.zip | |
Merge branch 'main' of https://github.com/pektezol/LeastPortals
Former-commit-id: af8d8680aafc3d662f8b53a4f50f0ea356b26c26
Diffstat (limited to '')
| -rw-r--r-- | backend/controllers/loginController.go | 48 |
1 files changed, 29 insertions, 19 deletions
diff --git a/backend/controllers/loginController.go b/backend/controllers/loginController.go index cfe086d..ae6e957 100644 --- a/backend/controllers/loginController.go +++ b/backend/controllers/loginController.go | |||
| @@ -17,13 +17,13 @@ import ( | |||
| 17 | 17 | ||
| 18 | // Login | 18 | // Login |
| 19 | // | 19 | // |
| 20 | // @Summary Get (redirect) login page for Steam auth. | 20 | // @Description Get (redirect) login page for Steam auth. |
| 21 | // @Tags login | 21 | // @Tags login |
| 22 | // @Accept json | 22 | // @Accept json |
| 23 | // @Produce json | 23 | // @Produce json |
| 24 | // @Success 200 {object} models.Response{data=models.LoginResponse} | 24 | // @Success 200 {object} models.Response{data=models.LoginResponse} |
| 25 | // @Failure 400 {object} models.Response | 25 | // @Failure 400 {object} models.Response |
| 26 | // @Router /login [get] | 26 | // @Router /login [get] |
| 27 | func Login(c *gin.Context) { | 27 | func Login(c *gin.Context) { |
| 28 | openID := steam_go.NewOpenId(c.Request) | 28 | openID := steam_go.NewOpenId(c.Request) |
| 29 | switch openID.Mode() { | 29 | switch openID.Mode() { |
| @@ -59,10 +59,20 @@ func Login(c *gin.Context) { | |||
| 59 | database.DB.Exec(`INSERT INTO users (steam_id, user_name, avatar_link, country_code) | 59 | database.DB.Exec(`INSERT INTO users (steam_id, user_name, avatar_link, country_code) |
| 60 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) | 60 | VALUES ($1, $2, $3, $4)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode) |
| 61 | } | 61 | } |
| 62 | moderator := false | ||
| 63 | rows, _ := database.DB.Query("SELECT title_name FROM titles WHERE user_id = $1", steamID) | ||
| 64 | for rows.Next() { | ||
| 65 | var title string | ||
| 66 | rows.Scan(&title) | ||
| 67 | if title == "Moderator" { | ||
| 68 | moderator = true | ||
| 69 | } | ||
| 70 | } | ||
| 62 | // Generate JWT token | 71 | // Generate JWT token |
| 63 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ | 72 | token := jwt.NewWithClaims(jwt.SigningMethodHS256, jwt.MapClaims{ |
| 64 | "sub": steamID, | 73 | "sub": steamID, |
| 65 | "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), | 74 | "exp": time.Now().Add(time.Hour * 24 * 30).Unix(), |
| 75 | "mod": moderator, | ||
| 66 | }) | 76 | }) |
| 67 | // Sign and get the complete encoded token as a string using the secret | 77 | // Sign and get the complete encoded token as a string using the secret |
| 68 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) | 78 | tokenString, err := token.SignedString([]byte(os.Getenv("SECRET_KEY"))) |
| @@ -85,13 +95,13 @@ func Login(c *gin.Context) { | |||
| 85 | 95 | ||
| 86 | // GET Token | 96 | // GET Token |
| 87 | // | 97 | // |
| 88 | // @Summary Gets the token cookie value from the user. | 98 | // @Description Gets the token cookie value from the user. |
| 89 | // @Tags auth | 99 | // @Tags auth |
| 90 | // @Produce json | 100 | // @Produce json |
| 91 | // | 101 | // |
| 92 | // @Success 200 {object} models.Response{data=models.LoginResponse} | 102 | // @Success 200 {object} models.Response{data=models.LoginResponse} |
| 93 | // @Failure 404 {object} models.Response | 103 | // @Failure 404 {object} models.Response |
| 94 | // @Router /token [get] | 104 | // @Router /token [get] |
| 95 | func GetCookie(c *gin.Context) { | 105 | func GetCookie(c *gin.Context) { |
| 96 | cookie, err := c.Cookie("token") | 106 | cookie, err := c.Cookie("token") |
| 97 | if err != nil { | 107 | if err != nil { |
| @@ -109,13 +119,13 @@ func GetCookie(c *gin.Context) { | |||
| 109 | 119 | ||
| 110 | // DELETE Token | 120 | // DELETE Token |
| 111 | // | 121 | // |
| 112 | // @Summary Deletes the token cookie from the user. | 122 | // @Description Deletes the token cookie from the user. |
| 113 | // @Tags auth | 123 | // @Tags auth |
| 114 | // @Produce json | 124 | // @Produce json |
| 115 | // | 125 | // |
| 116 | // @Success 200 {object} models.Response{data=models.LoginResponse} | 126 | // @Success 200 {object} models.Response{data=models.LoginResponse} |
| 117 | // @Failure 404 {object} models.Response | 127 | // @Failure 404 {object} models.Response |
| 118 | // @Router /token [delete] | 128 | // @Router /token [delete] |
| 119 | func DeleteCookie(c *gin.Context) { | 129 | func DeleteCookie(c *gin.Context) { |
| 120 | cookie, err := c.Cookie("token") | 130 | cookie, err := c.Cookie("token") |
| 121 | if err != nil { | 131 | if err != nil { |