From 637b656d36ec134d7e4d76a478045e562c8ae6df Mon Sep 17 00:00:00 2001 From: Derrick Hammer Date: Thu, 29 Jun 2023 05:41:26 -0400 Subject: [PATCH] refactor(auth): move getCurrentUserId to auth package and make public --- controller/controller.go | 14 -------------- controller/files.go | 2 +- service/auth/util.go | 14 ++++++++++++++ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/controller/controller.go b/controller/controller.go index b84527e..7ad659c 100644 --- a/controller/controller.go +++ b/controller/controller.go @@ -5,7 +5,6 @@ import ( "git.lumeweb.com/LumeWeb/portal/logger" "github.com/kataras/iris/v12" "go.uber.org/zap" - "strconv" ) func tryParseRequest(r interface{}, ctx iris.Context) (interface{}, bool) { @@ -71,16 +70,3 @@ func (c Controller) respondJSON(data interface{}) { logger.Get().Error("failed to generate response", zap.Error(err)) } } - -func getCurrentUserId(ctx iris.Context) uint { - usr := ctx.User() - - if usr == nil { - return 0 - } - - sid, _ := usr.GetID() - userID, _ := strconv.Atoi(sid) - - return uint(userID) -} diff --git a/controller/files.go b/controller/files.go index 4a9098f..ad3c3a0 100644 --- a/controller/files.go +++ b/controller/files.go @@ -141,7 +141,7 @@ func (f *FilesController) PostPinBy(cidString string) { return } - err := files.Pin(hashHex, getCurrentUserId(ctx)) + err := files.Pin(hashHex, auth.GetCurrentUserId(ctx)) if internalError(ctx, err) { logger.Get().Error(err.Error()) return diff --git a/service/auth/util.go b/service/auth/util.go index 6dcfd61..0abd2ce 100644 --- a/service/auth/util.go +++ b/service/auth/util.go @@ -9,6 +9,7 @@ import ( "github.com/kataras/jwt" "go.uber.org/zap" "golang.org/x/crypto/bcrypt" + "strconv" "strings" "time" ) @@ -110,3 +111,16 @@ func GetRequestAuthCode(ctx iris.Context) string { return authHeaderParts[1] } + +func GetCurrentUserId(ctx iris.Context) uint { + usr := ctx.User() + + if usr == nil { + return 0 + } + + sid, _ := usr.GetID() + userID, _ := strconv.Atoi(sid) + + return uint(userID) +}