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.
26 lines
554 B
26 lines
554 B
2 years ago
|
package middleware
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"git.diulo.com/mogfee/protoc-gen-kit/xuser"
|
||
|
"google.golang.org/grpc/metadata"
|
||
|
)
|
||
|
|
||
|
func JWT(authKey string) 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 := xuser.ValidateUser(ctx.Value(token).(string))
|
||
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
ctx = xuser.WithContext(ctx, userId)
|
||
|
return handler(ctx, a)
|
||
|
}
|
||
|
}
|
||
|
}
|