refactor(auth): move getCurrentUserId to auth package and make public

This commit is contained in:
Derrick Hammer 2023-06-29 05:41:26 -04:00
parent 5d15ca330a
commit 637b656d36
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
3 changed files with 15 additions and 15 deletions

View File

@ -5,7 +5,6 @@ import (
"git.lumeweb.com/LumeWeb/portal/logger" "git.lumeweb.com/LumeWeb/portal/logger"
"github.com/kataras/iris/v12" "github.com/kataras/iris/v12"
"go.uber.org/zap" "go.uber.org/zap"
"strconv"
) )
func tryParseRequest(r interface{}, ctx iris.Context) (interface{}, bool) { 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)) 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)
}

View File

@ -141,7 +141,7 @@ func (f *FilesController) PostPinBy(cidString string) {
return return
} }
err := files.Pin(hashHex, getCurrentUserId(ctx)) err := files.Pin(hashHex, auth.GetCurrentUserId(ctx))
if internalError(ctx, err) { if internalError(ctx, err) {
logger.Get().Error(err.Error()) logger.Get().Error(err.Error())
return return

View File

@ -9,6 +9,7 @@ import (
"github.com/kataras/jwt" "github.com/kataras/jwt"
"go.uber.org/zap" "go.uber.org/zap"
"golang.org/x/crypto/bcrypt" "golang.org/x/crypto/bcrypt"
"strconv"
"strings" "strings"
"time" "time"
) )
@ -110,3 +111,16 @@ func GetRequestAuthCode(ctx iris.Context) string {
return authHeaderParts[1] 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)
}