The Messenger of Allah ﷺ said, "You should not be impressed by anyone until you see how his life ends. A person may spend a period of his life, or a portion of his time, doing righteous deeds such that if he died upon them he would enter Paradise, then he changes and does evil deeds. And a servant may spend a portion of his time doing evil deeds such that if he died upon them he would enter the Fire, then he changes and does righteous deeds. When Allah intends good for a servant, He puts him to use before his death."The Companions asked, "Messenger of Allah, how does He put him to use?"He replied, "He grants him success in performing righteous deeds, then takes his soul while he is upon them."
حدثنا يزيد بن هارون أخبرنا حميد
أن رسول الله ﷺ قال لا عليكم أن لا تعجبوا بأحد حتى تنظروا بم يختم له فإن العامل يعمل زمانا من عمره أو برهة من دهره بعمل صالح لو مات عليه دخل الجنة ثم يتحول فيعمل عملا سيئا وإن العبد ليعمل البرهة من دهره بعمل سيئ لو مات عليه دخل النار ثم يتحول فيعمل عملا صالحا وإذا أراد الله بعبد خيرا استعمله قبل موته قالوا يا رسول الله وكيف يستعمله قال يوفقه لعمل صالح ثم يقبضه عليه
Its isnad is sahih. It is found in Bukhari, similar to this, 6/303, no. 3208, Fath, in the beginning of creation, mention of the angels, and Muslim, 4/2036, no. 2643, in decree, on the manner of the creation of man, and Tirmidhi, 4/446, no. 2137, who said, hasan sahih.