It has been narrated on the authority of 'Abd al-Rahman b. Abd Rabb al-Ka'ba who said:

I entered the mosque when 'Abdullah b. 'Amr b. al-'As was sitting in the shade of the Ka'ba and the people had gathered around him. I betook myself to them and sat near him. (Now) Abdullah said: I accompanied the Messenger of Allah (ﷺ) on a journey. We halted at a place. Some of us began to set right their tents, others began to compete with one another in shooting, and others began to graze their beasts, when an announcer of the Messenger of Allah (ﷺ) announced that the people should gather together for prayer, so we gathered around the Messenger of Allah (ﷺ). He said: It was the duty of every Prophet that has gone before me to guide his followers to what he knew was good for them and warn them against what he knew was bad for them; but this Umma of yours has its days of peace and (security) in the beginning of its career, and in the last phase of its existence it will be afflicted with trials and with things disagreeable to you. (In this phase of the Umma), there will be tremendous trials one after the other, each making the previous one dwindle into insignificance. When they would be afflicted with a trial, the believer would say: This is going to bring about my destruction. When at (the trial) is over, they would be afflicted with another trial, and the believer would say: This surely is going to be my end. Whoever wishes to be delivered from the fire and enter the garden should die with faith in Allah and the Last Day and should treat the people as he wishes to be treated by them. He who swears allegiance to a Caliph should give him the piedge of his hand and the sincerity of his heart (i. e. submit to him both outwardly as well as inwardly). He should obey him to the best of his capacity. It another man comes forward (as a claimant to Caliphate), disputing his authority, they (the Muslims) should behead the latter. The narrator says: I came close to him ('Abdullah b. 'Amr b. al-'As) and said to him: Can you say on oath that you heard it from the Messenger of Allah (ﷺ)? He pointed with his hands to his ears and his heart and said: My ears heard it and my mind retained it. I said to him: This cousin of yours, Mu'awiya, orders us to unjustly consume our wealth among ourselves and to kill one another, while Allah says:" O ye who believe, do not consume your wealth among yourselves unjustly, unless it be trade based on mutual agreement, and do not kill yourselves. Verily, God is Merciful to you" (iv. 29). The narrator says that (hearing this) Abdullah b. 'Amr b. al-As kept quiet for a while and then said: Obey him in so far as he is obedient to God; and diqobey him in matters involving disobedience to God.
حَدَّثَنَا زُهَيْرُ بْنُ حَرْبٍ، وَإِسْحَاقُ بْنُ إِبْرَاهِيمَ، قَالَ إِسْحَاقُ أَخْبَرَنَا وَقَالَ، زُهَيْرٌ حَدَّثَنَا جَرِيرٌ، عَنِ الأَعْمَشِ، عَنْ زَيْدِ بْنِ وَهْبٍ، عَنْ عَبْدِ الرَّحْمَنِ بْنِ عَبْدِ رَبِّ الْكَعْبَةِ، قَالَ دَخَلْتُ الْمَسْجِدَ فَإِذَا عَبْدُ اللَّهِ بْنُ عَمْرِو بْنِ الْعَاصِ جَالِسٌ فِي ظِلِّ الْكَعْبَةِ وَالنَّاسُ مُجْتَمِعُونَ عَلَيْهِ فَأَتَيْتُهُمْ فَجَلَسْتُ إِلَيْهِ فَقَالَ كُنَّا مَعَ رَسُولِ اللَّهِ صلى الله عليه وسلم فِي سَفَرٍ فَنَزَلْنَا مَنْزِلاً فَمِنَّا مَنْ يُصْلِحُ خِبَاءَهُ وَمِنَّا مَنْ يَنْتَضِلُ وَمِنَّا مَنْ هُوَ فِي جَشَرِهِ إِذْ نَادَى مُنَادِي رَسُولِ اللَّهِ صلى الله عليه وسلم الصَّلاَةَ جَامِعَةً ‏.‏ فَاجْتَمَعْنَا إِلَى رَسُولِ اللَّهِ صلى الله عليه وسلم فَقَالَ ‏"‏ إِنَّهُ لَمْ يَكُنْ نَبِيٌّ قَبْلِي إِلاَّ كَانَ حَقًّا عَلَيْهِ أَنْ يَدُلَّ أُمَّتَهُ عَلَى خَيْرِ مَا يَعْلَمُهُ لَهُمْ وَيُنْذِرَهُمْ شَرَّ مَا يَعْلَمُهُ لَهُمْ وَإِنَّ أُمَّتَكُمْ هَذِهِ جُعِلَ عَافِيَتُهَا فِي أَوَّلِهَا وَسَيُصِيبُ آخِرَهَا بَلاَءٌ وَأُمُورٌ تُنْكِرُونَهَا وَتَجِيءُ فِتْنَةٌ فَيُرَقِّقُ بَعْضُهَا بَعْضًا وَتَجِيءُ الْفِتْنَةُ فَيَقُولُ الْمُؤْمِنُ هَذِهِ مُهْلِكَتِي ‏.‏ ثُمَّ تَنْكَشِفُ وَتَجِيءُ الْفِتْنَةُ فَيَقُولُ الْمُؤْمِنُ هَذِهِ هَذِهِ ‏.‏ فَمَنْ أَحَبَّ أَنْ يُزَحْزَحَ عَنِ النَّارِ وَيَدْخُلَ الْجَنَّةَ فَلْتَأْتِهِ مَنِيَّتُهُ وَهُوَ يُؤْمِنُ بِاللَّهِ وَالْيَوْمِ الآخِرِ وَلْيَأْتِ إِلَى النَّاسِ الَّذِي يُحِبُّ أَنْ يُؤْتَى إِلَيْهِ وَمَنْ بَايَعَ إِمَامًا فَأَعْطَاهُ صَفْقَةَ يَدِهِ وَثَمَرَةَ قَلْبِهِ فَلْيُطِعْهُ إِنِ اسْتَطَاعَ فَإِنْ جَاءَ آخَرُ يُنَازِعُهُ فَاضْرِبُوا عُنُقَ الآخَرِ ‏"‏ ‏.‏ فَدَنَوْتُ مِنْهُ فَقُلْتُ لَهُ أَنْشُدُكَ اللَّهَ آنْتَ سَمِعْتَ هَذَا مِنْ رَسُولِ اللَّهِ صلى الله عليه وسلم فَأَهْوَى إِلَى أُذُنَيْهِ وَقَلْبِهِ بِيَدَيْهِ وَقَالَ سَمِعَتْهُ أُذُنَاىَ وَوَعَاهُ قَلْبِي ‏.‏ فَقُلْتُ لَهُ هَذَا ابْنُ عَمِّكَ مُعَاوِيَةُ يَأْمُرُنَا أَنْ نَأْكُلَ أَمْوَالَنَا بَيْنَنَا بِالْبَاطِلِ وَنَقْتُلَ أَنْفُسَنَا وَاللَّهُ يَقُولُ ‏{‏ يَا أَيُّهَا الَّذِينَ آمَنُوا لاَ تَأْكُلُوا أَمْوَالَكُمْ بَيْنَكُمْ بِالْبَاطِلِ إِلاَّ أَنْ تَكُونَ تِجَارَةً عَنْ تَرَاضٍ مِنْكُمْ وَلاَ تَقْتُلُوا أَنْفُسَكُمْ إِنَّ اللَّهَ كَانَ بِكُمْ رَحِيمًا‏}‏ قَالَ فَسَكَتَ سَاعَةً ثُمَّ قَالَ أَطِعْهُ فِي طَاعَةِ اللَّهِ وَاعْصِهِ فِي مَعْصِيَةِ اللَّهِ ‏.‏
Reference : Sahih Muslim 1844 a
In-book reference : Book 33, Hadith 74
USC-MSA web (English) reference : Book 20, Hadith 4546
  (deprecated numbering scheme)