package middleware import ( "context" "git.diulo.com/mogfee/kit/constants" "google.golang.org/grpc/metadata" ) type ValidateUser interface { ValidateUser(string) (int64, error) } func JWT(validate ValidateUser) Middleware { return func(handler Handler) Handler { return func(ctx context.Context, a any) (any, error) { var token string if md, ok := metadata.FromIncomingContext(ctx); ok { token = md.Get("token")[0] } userId, err := validate.ValidateUser(ctx.Value(token).(string)) if err != nil { return nil, err } ctx = context.WithValue(ctx, constants.UserIdKey{}, userId) return handler(ctx, a) } } }