There is no obedience in sinful matters
How do you reconcile between the following two hadiith:
It his been narrated through a different chain of transmitters, on the authority of Hudhaifa b. al-Yaman who said:
O Messenger of Allah, no doubt, we had an evil time (i. e. the days of Jahiliyya or ignorance) and God brought us a good time (i. e. Islamic period) through which we are now living Will there be a bad time after this good time?
He (the Holy Prophet) said: Yes.
I said: Will there be a good time after this bad time?
He said: Yes. I said: Will there be a bad time after good time?
He said: Yes. I said: How?
Whereupon he said: There will be leaders who will not be led by my guidance and who will not adopt my ways? There will be among them men who will have the hearts of devils in the bodies of human beings.
I said: What should I do O Messenger of Allah, if I (happen) to live in that time?
He replied: You will listen to the Amir and carry out his orders; even if your back is flogged and your wealth is snatched, you should listen and obey.
[Sahih Muslim Hadith No. 1847]
Narrated `Ali:
The Prophet (sallallaahu alayhi wa sallam) , sent an army and appointed some man their commander The man made a fire and then said (to the soldiers), "Enter it." Some of them intended to enter it while some others said, 'We have run away from it (i.e., embraced Islam to save ourselves from the 'fire')." They mentioned that to the Prophet, and he said about people who had intended to enter the fire. ''If they had entered it, they would have remained In it till the Day of Resurrection.'' Then he said to others, "There is no obedience for evil deeds, obedience is required only in what is good."
[Sahih al-Bukhari Hadith No. 7257]
The second hadiith does not mean that you obey a leader if he orders you to do something sinful.
It means that you obey him when he orders you to do that which is right even if he himself is a tyrant.
Sheikh Ibn Uthaymiin said in the explanation of the following hadiith:
On the authority of Abu Najeeh al-’Irbaad ibn Saariyah (may Allah be pleased with him) who said:
The Messenger of Allah (sallallaahu alayhi wa sallam) gave us a sermon by which our hearts were filled with fear and tears came to our eyes. So we said, "O Messenger of Allah! It is as though this is a farewell sermon, so counsel us."
He (sallallaahu alayhi wa sallam) said, "I counsel you to have taqwa (fear) of Allah, and to listen and obey [your leader], even if a slave were to become your ameer. Verily he among you who lives long will see great controversy, so you must keep to my Sunnah and to the Sunnah of the Khulafa ar-Rashideen (the rightly guided caliphs), those who guide to the right way. Cling to it stubbornly [literally: with your molar teeth].
Beware of newly invented matters [in the religion], for verily every bidah (innovation) is misguidance."
[Paraphrase] The apparent meaning of the hadiith is that it is obligatory to listen to and obey the leader, even if he disobeys Allaah, as long as he does not order you to do something which is counted as disobedience of Allaah because the Prophet (sallallaahu alayhi wa sallam) said listen and obey [to your leader] even if your back is flogged and your wealth is snatched. And the back being flogged and wealth being snatched, without a valid legislative reason, is without a doubt disobedience. A person cannot say to the leader: I wil not obey you until you obey your Lord. This is impermissible. Rather, it is obligatory for him to obey him even if he [the leader] does not obey his Lord. But if he orders you to do something haraam then you are not to listen or obey.
وظاهر الحديث وجوب السمع والطاعة لولي الأمر، وإن كان يعصي الله عزّ وجل، إذا لم يأمرك بمعصية الله عزّ وجل؛ لأن النبي صلى الله عليه وسلم قال: اسمَع وَأَطِع، وَإِن ضَرَبَ ظَهرَكَ، وَأَخَذَ مَالَكَ ـ وضرب الظهر، وأخذ المال بلا سبب شرعي، معصية لا شك، فلا يقول الإنسان لولي الأمر: أنا لا أطيعك حتى تطيع ربك، فهذا حرام، بل يجب أن يطيعه، وإن لم يطع ربه، أما لو أمر بالمعصية، فلا سمع، ولا طاعة؛ لأن رب ولي الأمر ورب الرعية، واحد عزّ وجل، فكلهم يجب أن يخضعوا له عزّ وجل، فإذا أمرنا بمعصية الله قلنا: لا سمع، ولا طاعة
Based on this explanation it should be evident to you, the reader, that there is no contradiction between the two hadiith.
You obey your leader as long as he does not command you to do something sinful even if he is a sinner himself.
You do not obey your leader when he orders you to do something which is sinful.