In narrow contexts, yes.
The mere fact of a mental illness obviously should not bar someone with no history of violence from owning a gun.
However, in cases where a person has a mental illness that makes them immediately dangerous, such as psychopathy, sadism, etc., perhaps that should be taken into account. I'd say that it should still not be enough to actually on its own bar ownership, but perhaps we should have a "red flag" system where too many "red flags" bar one from purchasing a gun or using it without strict monitoring. That having been said, someone who is medicated and has a note from an appropriate professional that they are successfully managing their condition should have such a "red flag" removed.
But the vast majority of the mentally ill are no threat to themselves or others, especially if they get proper treatment. My number one concern in thinking about this question is actually stopping depressed people from getting easy access to firearms for the purposes of suicide. But a suicidal person will find a way no matter what laws we would enact, and the civil liberties costs would be too high. Still, perhaps waiting periods should be extended for people with a history of suicide and no proof of current treatment. This would require better databases, but that is going to be something we need anyways.