aboutsummaryrefslogtreecommitdiff
path: root/backend
diff options
context:
space:
mode:
Diffstat (limited to 'backend')
-rw-r--r--backend/controllers/controllers.go74
-rw-r--r--backend/routes/routes.go2
2 files changed, 74 insertions, 2 deletions
diff --git a/backend/controllers/controllers.go b/backend/controllers/controllers.go
index 6a38e8c..712b33c 100644
--- a/backend/controllers/controllers.go
+++ b/backend/controllers/controllers.go
@@ -4,11 +4,13 @@ import (
4 "log" 4 "log"
5 "net/http" 5 "net/http"
6 "os" 6 "os"
7 "regexp"
7 "time" 8 "time"
8 9
9 "github.com/gin-gonic/gin" 10 "github.com/gin-gonic/gin"
10 "github.com/golang-jwt/jwt/v4" 11 "github.com/golang-jwt/jwt/v4"
11 "github.com/pektezol/leastportals/backend/database" 12 "github.com/pektezol/leastportals/backend/database"
13 "github.com/pektezol/leastportals/backend/models"
12 "github.com/solovev/steam_go" 14 "github.com/solovev/steam_go"
13) 15)
14 16
@@ -46,7 +48,7 @@ func Login(c *gin.Context) {
46 } 48 }
47 // Insert new user to database 49 // 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) 50 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.Avatar, user.LocCountryCode, time.Now().UTC(), time.Now().UTC(), 0) 51 VALUES ($1, $2, $3, $4, $5, $6, $7)`, steamID, user.PersonaName, user.AvatarFull, user.LocCountryCode, time.Now().UTC(), time.Now().UTC(), 0)
50 } 52 }
51 // Update updated_at 53 // Update updated_at
52 database.DB.Exec(`UPDATE users SET updated_at = $1 WHERE steam_id = $2`, time.Now().UTC(), steamID) 54 database.DB.Exec(`UPDATE users SET updated_at = $1 WHERE steam_id = $2`, time.Now().UTC(), steamID)
@@ -84,6 +86,74 @@ func Logout(c *gin.Context) {
84 c.JSON(http.StatusOK, gin.H{ 86 c.JSON(http.StatusOK, gin.H{
85 "output": "logout success", 87 "output": "logout success",
86 }) 88 })
87 //c.Redirect(http.StatusPermanentRedirect, "/")
88 } 89 }
89} 90}
91
92func Profile(c *gin.Context) {
93 // Check if user exists
94 user, exists := c.Get("user")
95 if !exists {
96 c.JSON(http.StatusUnauthorized, gin.H{
97 "code": http.StatusUnauthorized,
98 "output": gin.H{
99 "error": "User not logged in. Could be invalid token.",
100 },
101 })
102 } else {
103 user := user.(models.User)
104 c.JSON(http.StatusOK, gin.H{
105 "code": http.StatusOK,
106 "output": gin.H{
107 "username": user.Username,
108 "avatar": user.AvatarLink,
109 "types": user.TypeToString(),
110 },
111 "profile": true,
112 })
113 }
114}
115
116func User(c *gin.Context) {
117 id := c.Param("id")
118 // Check if id is all numbers and 17 length
119 match, _ := regexp.MatchString("^[0-9]{17}$", id)
120 if !match {
121 c.JSON(http.StatusNotFound, gin.H{
122 "code": http.StatusNotFound,
123 "output": gin.H{
124 "error": "User not found.",
125 },
126 })
127 return
128 }
129 // Check if user exists
130 var targetUser models.User
131 database.DB.QueryRow(`SELECT * FROM users WHERE steam_id = $1;`, id).Scan(
132 &targetUser.SteamID, &targetUser.Username, &targetUser.AvatarLink, &targetUser.CountryCode,
133 &targetUser.CreatedAt, &targetUser.UpdatedAt, &targetUser.UserType)
134 if targetUser.SteamID == "" {
135 // User does not exist
136 c.JSON(http.StatusNotFound, gin.H{
137 "code": http.StatusNotFound,
138 "output": gin.H{
139 "error": "User not found.",
140 },
141 })
142 return
143 }
144 // Target user exists
145 _, exists := c.Get("user")
146 if exists {
147 c.Redirect(http.StatusFound, "/api/v1/profile")
148 return
149 }
150 c.JSON(http.StatusOK, gin.H{
151 "code": http.StatusOK,
152 "output": gin.H{
153 "username": targetUser.Username,
154 "avatar": targetUser.AvatarLink,
155 "types": targetUser.TypeToString(),
156 },
157 "profile": false,
158 })
159}
diff --git a/backend/routes/routes.go b/backend/routes/routes.go
index 1f9661c..dee43e7 100644
--- a/backend/routes/routes.go
+++ b/backend/routes/routes.go
@@ -13,5 +13,7 @@ func InitRoutes(router *gin.Engine) {
13 v1.GET("/", middleware.CheckAuth, controllers.Home) 13 v1.GET("/", middleware.CheckAuth, controllers.Home)
14 v1.GET("/login", controllers.Login) 14 v1.GET("/login", controllers.Login)
15 v1.GET("/logout", middleware.CheckAuth, controllers.Logout) 15 v1.GET("/logout", middleware.CheckAuth, controllers.Logout)
16 v1.GET("/profile", middleware.CheckAuth, controllers.Profile)
17 v1.GET("/user/:id", middleware.CheckAuth, controllers.User)
16 } 18 }
17} 19}