From e616df98557fe57fc942c360f03f7fafbdb965bd Mon Sep 17 00:00:00 2001 From: Vidhu Kant Sharma Date: Fri, 5 Apr 2024 20:05:11 +0530 Subject: added functionality to get logged in user's data --- main.go | 4 ++-- user/controller.go | 27 ++++++++++++++++++++------- 2 files changed, 22 insertions(+), 9 deletions(-) diff --git a/main.go b/main.go index 958f939..ff12523 100644 --- a/main.go +++ b/main.go @@ -38,7 +38,7 @@ import ( "log" ) -const OPENBILLS_VERSION = "v0.5.0" +const OPENBILLS_VERSION = "v0.6.0" func init() { if !viper.GetBool("debug_mode") { @@ -55,13 +55,13 @@ func main() { api := r.Group("/api") api.Use(errors.ErrResponse()) { - user.Routes(api) auth.Routes(api) } protected := api.Group("/") protected.Use(auth.Authorize()) { + user.Routes(protected) customer.Routes(protected) item.Routes(protected) invoice.Routes(protected) diff --git a/user/controller.go b/user/controller.go index 4933ea3..15061cc 100644 --- a/user/controller.go +++ b/user/controller.go @@ -1,5 +1,5 @@ /* openbills - Server for web based Libre Billing Software - * Copyright (C) 2023 Vidhu Kant Sharma + * Copyright (C) 2023-2024 Vidhu Kant Sharma * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -18,6 +18,7 @@ package user import ( + e "vidhukant.com/openbills/errors" "github.com/gin-gonic/gin" "net/http" ) @@ -25,12 +26,24 @@ import ( func handleGetUser (ctx *gin.Context) { var user User - //err = getUser(&user, uint(id)) - //if err != nil { - // ctx.Error(err) - // ctx.Abort() - // return - //} + uId, ok := ctx.Get("UserID") + if !ok { + ctx.Error(e.ErrUnauthorized) + ctx.Abort() + return + } + + userId := uId.(uint) + + err := GetUser(&user, userId) + if err != nil { + ctx.Error(err) + ctx.Abort() + return + } + + // remove password hash from response + user.Password = "" ctx.JSON(http.StatusOK, gin.H{ "message": "success", -- cgit v1.2.3