From cc79129d6a246f8d4cb911e1edd22066a3623b0b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E4=BC=9F=E4=B9=90?= Date: Wed, 1 Mar 2023 15:12:02 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E6=B3=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + xuser/xuser.go | 17 ++++++----------- 2 files changed, 7 insertions(+), 11 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/xuser/xuser.go b/xuser/xuser.go index 1a2bdb2..adbcf0d 100644 --- a/xuser/xuser.go +++ b/xuser/xuser.go @@ -1,23 +1,18 @@ package xuser import ( + "context" "github.com/gin-gonic/gin" ) -type requestUserIdKey struct { -} - -func GetUserNameFromContext(ctx *gin.Context) string { - v, ok := ctx.Get("userId") - if !ok { +func GetUserFromContext(ctx context.Context) string { + v := ctx.Value("username") + if v == nil { return "" } - //if value, ok := ctx.Value(requestUserIdKey{}).(string); ok { - // return value - //} return v.(string) } -func SetContextUserId(ctx *gin.Context, username string) { - ctx.Set("userId", username) +func SetContextUser(ctx *gin.Context, username string) { + ctx.Set("username", username) //return context.WithValue(ctx, requestUserIdKey{}, username) }