عَنْ هِشَامِ بْنِ سَالِمٍ عَنْ حَبِيبٍ السِّجِسْتَانِيِّ عَنْ أَبِي جَعْفَرٍ عَلَيْهِ السَّلَامُ قَالَ قَالَ اللهُ تَبَارَكَ وَ تَعَالَى لَأُعَذِّبَنَّ كُلَّ رَعِيَّةٍ فِي الْإِسْلَامِ دَانَتْ بِوَلَايَةِ كُلِّ إِمَامٍ جَائِرٍ لَيْسَ مِنَ اللهِ وَ إِنْ كَانَتِ الرَّعِيَّةُ فِي أَعْمَالِهَا بَرَّةً تَقِيَّةً وَ لَأَعْفُوَنَّ عَنْ كُلِّ رَعِيَّةٍ فِي الْإِسْلَامِ دَانَتْ بِوَلَايَةِ كُلِّ إِمَامٍ عَادِلٍ مِنَ اللهِ وَ إِنْ كَانَتِ الرَّعِيَّةُ فِي أَنْفُسِهَا ظَالِمَةً مُسِيئَةً
Heshaam Ibn Saalim (al-Jawaaleeqi) reports from Habeeb al-Sajistaani that Imam Abu Ja’far (Muhammad al-Baqer) (peace be on him) said, “Allah – Blessed and High be He – said, ‘Certainly, I will punish every nation in Islam that believes in the mastership of an unjust Imam not from Allah (i.e. appointed by Allah), even if the nation, in its deeds is pious and good. And definitely, I will pardon every nation in Islam that believes in the mastership of a just Imam from Allah, even if the nation in itself is unjust and sinful.”
References:
1. Al-Kaafi, vol. 1, p. 376, H. 4
2. Al-Ikhtesaas, p. 259
3. A’laam al-Deen, p. 400
4. Behaar al-Anwaar, vol. 25, p. 110, H. 1 and vol. 27, p. 193, H. 51
5. Mustadrak al-Wasaael, vol. 18, p. 175, H. 22426
Notes:
1) This narration underlines the importance of having the correct beliefs, especially pertaining to Imamat because only if one believes in the rightful Imam appointed by Allah the High, he will have the correct beliefs in monotheism, prophethood, resurrection and even branches of religion like prayers, fasting, etc.
2) Correct beliefs always leave scope for forgiveness in acts of worship but this does not hold true in converse i.e. acts of worship without correct beliefs are meaningless. Hence, if a person spends his whole life in worshipping Allah without having the correct beliefs, his life will be a waste.