You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

32 lines
626 B

2 years ago
package xuser
import (
"context"
"github.com/gin-gonic/gin"
)
func GetUserFromContext(ctx context.Context) string {
v := ctx.Value("username")
if v == nil {
return ""
}
return v.(string)
}
func SetContextUser(ctx *gin.Context, username string) {
ctx.Set("username", username)
//return context.WithValue(ctx, requestUserIdKey{}, username)
}
type userIdKey struct {
}
func WithContext(ctx context.Context, userId int64) context.Context {
return context.WithValue(ctx, userIdKey{}, userId)
}
func FromContext(ctx context.Context) int64 {
if v, ok := ctx.Value(userIdKey{}).(int64); ok {
return v
}
return 0
}