Praise be to Allah.
He must hasten to repent, even if it is at the time of death, because the door of repentance is always open so long as he is of sound mind. He must hasten to repent and beware of sin, even if they have told him that the time he has left to live is short, because life spans are in the hand of Allah. The doctors may be mistaken in their thinking, and he may live for a long time. Whatever the case, he must hasten to repent and be sincere in doing so, so that Allah will accept his repentance. Allah says (interpretation of the meaning):
“And all of you beg Allah to forgive you all, O believers, that you may be successful” [al-Noor 24:31]
“And verily, I am indeed forgiving to him who repents, believes (in My Oneness, and associates none in worship with Me) and does righteous good deeds, and then remains constant in doing them (till his death)” [Ta-Ha 20:82]
And the Prophet (peace and blessings of Allah be upon him) said:
“Allah will accept a person's repentance so long as the death rattle has not reached his throat” i.e., up until the time when the dying person loses consciousness. And Allah is the One Whose help we seek.