aboutsummaryrefslogtreecommitdiff
path: root/backend/handlers/user.go
diff options
context:
space:
mode:
authorArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-10-31 22:06:00 +0300
committerArda Serdar Pektezol <1669855+pektezol@users.noreply.github.com>2024-10-31 22:06:00 +0300
commit4210c9b38f9053f6720a6bebaadefd24c542eaa9 (patch)
tree5b0061e23cf91291ed9e5f387766148d45103591 /backend/handlers/user.go
parentchore: change repo name to lphub (diff)
downloadlphub-4210c9b38f9053f6720a6bebaadefd24c542eaa9.tar.gz
lphub-4210c9b38f9053f6720a6bebaadefd24c542eaa9.tar.bz2
lphub-4210c9b38f9053f6720a6bebaadefd24c542eaa9.zip
backend: better auth check, audit logging
Diffstat (limited to 'backend/handlers/user.go')
-rw-r--r--backend/handlers/user.go28
1 files changed, 3 insertions, 25 deletions
diff --git a/backend/handlers/user.go b/backend/handlers/user.go
index 021a47f..53f0d06 100644
--- a/backend/handlers/user.go
+++ b/backend/handlers/user.go
@@ -69,12 +69,7 @@ type ScoreResponse struct {
69// @Success 200 {object} models.Response{data=ProfileResponse} 69// @Success 200 {object} models.Response{data=ProfileResponse}
70// @Router /profile [get] 70// @Router /profile [get]
71func Profile(c *gin.Context) { 71func Profile(c *gin.Context) {
72 // Check if user exists 72 user, _ := c.Get("user")
73 user, exists := c.Get("user")
74 if !exists {
75 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
76 return
77 }
78 // Get user links 73 // Get user links
79 links := models.Links{} 74 links := models.Links{}
80 sql := `SELECT u.p2sr, u.steam, u.youtube, u.twitch FROM users u WHERE u.steam_id = $1` 75 sql := `SELECT u.p2sr, u.steam, u.youtube, u.twitch FROM users u WHERE u.steam_id = $1`
@@ -699,15 +694,9 @@ func FetchUser(c *gin.Context) {
699// @Success 200 {object} models.Response{data=ProfileResponse} 694// @Success 200 {object} models.Response{data=ProfileResponse}
700// @Router /profile [post] 695// @Router /profile [post]
701func UpdateUser(c *gin.Context) { 696func UpdateUser(c *gin.Context) {
702 // Check if user exists 697 user, _ := c.Get("user")
703 user, exists := c.Get("user")
704 if !exists {
705 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
706 return
707 }
708 profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY")) 698 profile, err := GetPlayerSummaries(user.(models.User).SteamID, os.Getenv("API_KEY"))
709 if err != nil { 699 if err != nil {
710 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateSummaryFail, err.Error())
711 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 700 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
712 return 701 return
713 } 702 }
@@ -715,11 +704,9 @@ func UpdateUser(c *gin.Context) {
715 sql := `UPDATE users SET user_name = $1, avatar_link = $2, country_code = $3, updated_at = $4 WHERE steam_id = $5` 704 sql := `UPDATE users SET user_name = $1, avatar_link = $2, country_code = $3, updated_at = $4 WHERE steam_id = $5`
716 _, err = database.DB.Exec(sql, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID) 705 _, err = database.DB.Exec(sql, profile.PersonaName, profile.AvatarFull, profile.LocCountryCode, time.Now().UTC(), user.(models.User).SteamID)
717 if err != nil { 706 if err != nil {
718 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateFail, "UPDATE#users: "+err.Error())
719 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 707 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
720 return 708 return
721 } 709 }
722 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateSuccess)
723 c.JSON(http.StatusOK, models.Response{ 710 c.JSON(http.StatusOK, models.Response{
724 Success: true, 711 Success: true,
725 Message: "Successfully updated user.", 712 Message: "Successfully updated user.",
@@ -744,33 +731,24 @@ func UpdateUser(c *gin.Context) {
744// @Success 200 {object} models.Response 731// @Success 200 {object} models.Response
745// @Router /profile [put] 732// @Router /profile [put]
746func UpdateCountryCode(c *gin.Context) { 733func UpdateCountryCode(c *gin.Context) {
747 // Check if user exists 734 user, _ := c.Get("user")
748 user, exists := c.Get("user")
749 if !exists {
750 c.JSON(http.StatusOK, models.ErrorResponse("User not logged in."))
751 return
752 }
753 code := c.Query("country_code") 735 code := c.Query("country_code")
754 if code == "" { 736 if code == "" {
755 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail)
756 c.JSON(http.StatusOK, models.ErrorResponse("Enter a valid country code.")) 737 c.JSON(http.StatusOK, models.ErrorResponse("Enter a valid country code."))
757 return 738 return
758 } 739 }
759 var validCode string 740 var validCode string
760 err := database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1`, code).Scan(&validCode) 741 err := database.DB.QueryRow(`SELECT country_code FROM countries WHERE country_code = $1`, code).Scan(&validCode)
761 if err != nil { 742 if err != nil {
762 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail)
763 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 743 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
764 return 744 return
765 } 745 }
766 // Valid code, update profile 746 // Valid code, update profile
767 _, err = database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, validCode, user.(models.User).SteamID) 747 _, err = database.DB.Exec(`UPDATE users SET country_code = $1 WHERE steam_id = $2`, validCode, user.(models.User).SteamID)
768 if err != nil { 748 if err != nil {
769 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountryFail)
770 c.JSON(http.StatusOK, models.ErrorResponse(err.Error())) 749 c.JSON(http.StatusOK, models.ErrorResponse(err.Error()))
771 return 750 return
772 } 751 }
773 CreateLog(user.(models.User).SteamID, LogTypeUser, LogDescriptionUserUpdateCountrySuccess)
774 c.JSON(http.StatusOK, models.Response{ 752 c.JSON(http.StatusOK, models.Response{
775 Success: true, 753 Success: true,
776 Message: "Successfully updated country code.", 754 Message: "Successfully updated country code.",