Help with stepper motor speed base on the encoder

Can someone point me in the right direction.
I am able to read the encode and move the motor base on the encoder. The problem I have is I can't figure out how to move the motor at the same speed as the encoder.

Right now I have to hard code the speed at 500rpm: stepper.setSpeed(500);
but I want the speed of the motor to be dynamic (base on how fast the encoder moves).

#include <Stepper.h>

 
volatile boolean fired;
volatile boolean up;

// Encoder sensor pins
#define PINA 2
#define PINB 3
#define INTERRUPT 0  // that is, pin 2

// Motor pins
const int dirPin = 8; 
const int stepperPin = 7;

// the number of steps on your motor
#define STEPS 200
Stepper stepper(STEPS, 7, 8);


void interruptServiceRoutine (){
  pinMode(dirPin, OUTPUT);
  pinMode(stepperPin, OUTPUT);
 
  if (digitalRead(PINA)){
    up = digitalRead (PINB);
  }else{
    up = !digitalRead (PINB);
  }

  fired = true;
} 

void setup (){

  digitalWrite (PINA, HIGH); 
  digitalWrite (PINB, HIGH);
  attachInterrupt (INTERRUPT, interruptServiceRoutine, CHANGE); 

  Serial.begin (115200);

} 


void loop (){

  stepper.setSpeed(500); 

  if (fired){

    if (up){
       stepper.step(-1);
    }else{
       stepper.step(1);
    }
    fired = false;    

  }
      
}

Why do you think that you need an encoder? The stepper motor angle increases or decreases with every step, so you only need to track the steps already taken.

When you know the time to move to a new position, calculate the according rpm and call stepper.setSpeed(rpms) before stepper.step.

If you have an encoder that is moved by something (perhaps by your hand) and if you want the stepper motor to take a step every time the encoder produces a pulse you do not use the speed setting for the stepper library. You just write code to produce a single step.

...R
Stepper Motor Basics
Simple Stepper Program