sing/common/auth/context.go
2022-07-17 14:39:25 +08:00

14 lines
319 B
Go

package auth
import "context"
type userKey struct{}
func ContextWithUser[T any](ctx context.Context, user T) context.Context {
return context.WithValue(ctx, (*userKey)(nil), user)
}
func UserFromContext[T any](ctx context.Context) (T, bool) {
user, loaded := ctx.Value((*userKey)(nil)).(T)
return user, loaded
}