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.

29 lines
660 B

2 years ago
package middleware
import (
"context"
2 years ago
"git.diulo.com/mogfee/protoc-gen-kit/constants"
2 years ago
"google.golang.org/grpc/metadata"
)
2 years ago
type ValidateUser interface {
ValidateUser(string) (int64, error)
}
func JWT(validate ValidateUser) Middleware {
2 years ago
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]
}
2 years ago
userId, err := validate.ValidateUser(ctx.Value(token).(string))
2 years ago
if err != nil {
return nil, err
}
2 years ago
ctx = context.WithValue(ctx, constants.UserIdKey{}, userId)
2 years ago
return handler(ctx, a)
}
}
}