package transport import ( "context" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/status" ) type ReqWithToken interface { GetToken() string } func (s *AuthServer) AuthInterceptor() grpc.UnaryServerInterceptor { return func(ctx context.Context, req interface{}, info *grpc.UnaryServerInfo, handler grpc.UnaryHandler) (interface{}, error) { reqWithToken, ok := req.(ReqWithToken) if !ok { return handler(ctx, req) } token := reqWithToken.GetToken() userId, err := s.sessionService.Read(ctx, token) if err != nil { return nil, status.Errorf(codes.Unauthenticated, "") } user, err := s.userService.ReadUser(ctx, *userId) if err != nil { return nil, status.Errorf(codes.Unauthenticated, "") } ctx = context.WithValue(ctx, "user", user) return handler(ctx, req) } }