It is narrated from Aisha (may Allah be pleased with her) that a man said to the Messenger of Allah (ﷺ) while he was standing at the door and I was listening: "O Messenger of Allah (ﷺ)! I wake up in a state of major ritual impurity (janabah) and I intend to fast." The Messenger of Allah (ﷺ) said: "I also wake up in a state of janabah and I intend to fast. I perform ghusl first, and then I fast that day."
Hadith Referenceمسند الإمام الشافعي / كتاب الصيام / 648
Hadith Takhrijاخرجه مسلم، الصيام، باب صحة صوم من طلع عليه الفجر وهو جنب (1110)۔