As long as your state or Jurisdiction allows the carrying of pepper spray then it's not illegal to spray someone with pepper spray in self defense.
If someone is trying to rob you or attack you and you have pepper spray and can safely spray them then do so because it will stop the attack.
You should not get into trouble for pepper spraying someone in self defense when it's actually needed.
Now if you just go around spraying people with pepper spray then you could get into trouble and that would be illegal.
But in self defense it's not illegal to spray someone with pepper spray as you have the right to defend yourself from attack.
Pepper spray will also stop a dog attack so it's good to have pepper spray with you and I would not live anywhere that made pepper spray illegal.