summaryrefslogtreecommitdiff
path: root/user
diff options
context:
space:
mode:
authorVidhu Kant Sharma <vidhukant@vidhukant.xyz>2023-01-28 23:32:41 +0530
committerVidhu Kant Sharma <vidhukant@vidhukant.xyz>2023-01-28 23:32:41 +0530
commit0607478f1e4c86619a606af7876a6625e859ee1a (patch)
tree308c7a03e911994452a903d029c8cacd9a824d32 /user
parent31e9605652faf350291634f5a2d642573f320e66 (diff)
created endpoint to get logged in user's info
Diffstat (limited to 'user')
-rw-r--r--user/db_actions.go12
-rw-r--r--user/router.go28
-rw-r--r--user/user.go2
3 files changed, 40 insertions, 2 deletions
diff --git a/user/db_actions.go b/user/db_actions.go
index 2d89b7e..51490e7 100644
--- a/user/db_actions.go
+++ b/user/db_actions.go
@@ -46,3 +46,15 @@ func modifyUser(id primitive.ObjectID, nu User) error {
_, err := db.UpdateOne(context.TODO(), bson.D{{"_id", id}}, bson.D{{"$set", nu}})
return err
}
+
+// gets user info
+func getUser(userId primitive.ObjectID) (User, error) {
+ var user User
+ err := db.FindOne(context.TODO(), bson.D{{"_id", userId}}).Decode(&user)
+
+ // remove sensitive data
+ user.Password = ""
+ user.Sessions = []Session{}
+
+ return user, err
+}
diff --git a/user/router.go b/user/router.go
index 15d6efb..6e84185 100644
--- a/user/router.go
+++ b/user/router.go
@@ -18,8 +18,11 @@
package user
import (
+ "github.com/MikunoNaka/OpenBills-server/util"
+ "errors"
"github.com/gin-gonic/gin"
"go.mongodb.org/mongo-driver/bson/primitive"
+ "go.mongodb.org/mongo-driver/mongo"
"log"
"net/http"
)
@@ -28,15 +31,36 @@ import (
func Routes(route *gin.Engine) {
u := route.Group("/user")
{
+ u.GET("/", util.Authorize(), func(ctx *gin.Context) {
+ hex := ctx.MustGet("userId").(string)
+ id, err := primitive.ObjectIDFromHex(hex)
+ if err != nil {
+ ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
+ log.Printf("ERROR: Failed to modify user, Error parsing ID: %v\n", err.Error())
+ return
+ }
+
+ user, err := getUser(id)
+ if err != nil {
+ log.Printf("ERROR: Failed to read user %d info from DB: %v\n", id, err.Error())
+ if errors.Is(err, mongo.ErrNoDocuments) {
+ ctx.AbortWithStatusJSON(http.StatusNotFound, gin.H{"error": err.Error()})
+ } else {
+ ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": err.Error()})
+ }
+ }
+
+ ctx.JSON(http.StatusOK, user)
+ })
+
u.POST("/new", validateMiddleware(), func(ctx *gin.Context) {
u := ctx.MustGet("user").(User)
// TODO: maybe add an invite code for some instances
_, err := saveUser(u)
if err != nil {
- ctx.JSON(http.StatusInternalServerError, gin.H{"error": "could not login"})
log.Printf("ERROR: Failed to add new user %v to DB: %v\n", u, err.Error())
- return
+ ctx.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "could not login"})
}
log.Printf("Successfully saved new user to DB: %s", u.UserName)
diff --git a/user/user.go b/user/user.go
index 4c41f24..30ae333 100644
--- a/user/user.go
+++ b/user/user.go
@@ -28,6 +28,8 @@ var db *mongo.Collection = database.DB.Collection("Users")
// per-user config can be shared to DB
type Config struct {
+ // just CSS variable overrides for the frontend
+ Styling string `bson:"Styling" json:"Styling"`
}
type Session struct {