Imam Ali (AS) said that true happiness is that one’s ending is good and the true misery is when one’s ending is bad.
That is why men of God have always worried about whether or not they would have a good ending.
Imam Sajjad (AS) says in one of his supplications: O God! Give me a faith that is not temporary so that it would continue until death and when I come to you (Day of Resurrection).
In some supplications, we see that men of God talk about their beliefs and ask Gd to remind them of these belief when they are dying.
The Holy Quran says: “God will not forgive or guide to the right path those who first believe, then disbelieve, again believe and disbelieve, and then increase their disbelief.” (Verse 137 of Surah An-Nisa)
The Quran also says those who are ‘wellgrounded in knowledge’ are worried about deviations on the path and pray to God: “"Lord, do not cause our hearts to deviate from Your guidance, and grant us mercy. You are the Most Awarding One.” (Verse 8 of Surah Al Imran)
One of God’s blessings for them is that He keeps them steadfast in faith at the time of death: “God strengthens the faith of the believers by the true Words in this world and in the life to come. He causes the unjust to go astray and does whatever He pleases.” (Verse 27 of Surah Ibrahim)
Abraham (AS) and Jacob (AS) advice their children to be submissive to God: “God has chosen this religion for you. You must not leave this world unless you are a Muslim (submitted to the will of the Lord of the Universe).” (Verse 132 of Surah Al-Baqarah)
And after ordering believers to have Taqwa (God-fearing), God says: “… you should and die only as Muslims (having submitted to the will of God).” (Verse 102 of Surah Al-Imran)
One of the examples of bad ending that the Quran refers to is the case of Iblis (Satan). Iblis, who had worshipped god for 6,000 years refused to prostrate before Adam (AS) and ruined all of what he had done over those many years.
Another example of bad ending mentioned in the Quran is the case of Balaam, the son of Beor. He was a scholar who lived during the prophethood of Moses (AS). Balaam worshipped God so much that his prayers were answered and he was chosen as a preacher by Moses (AS). However, due to his inclination toward pharaoh, Balaam went astray and turned into an opponent of Moses (AS). His story is mentioned briefly in Surah Al-A’raf.