The Messenger of Allah ﷺ came and found that the doors of his companions' houses opened directly into the mosque. He said, "Turn these houses away from the mosque."Then the Prophet ﷺ went inside, and the people did nothing, hoping that some concession would be revealed concerning them. He came out to them again and said, "Turn these houses away from the mosque, for I do not permit the mosque to a menstruating woman or to anyone in a state of major ritual impurity."
حدثنا مسدد حدثنا عبد الواحد بن زياد حدثنا الأفلت بن خليفة قال حدثتني جسرة بنت دجاجة قالت سمعت
جاء رسول الله ﷺ ووجوه بيوت أصحابه شارعة في المسجد فقال وجهوا هذه البيوت عن المسجد ثم دخل النبي ﷺ ولم يصنع القوم شيئا رجاء أن تنزل فيهم رخصة فخرج إليهم بعد فقال وجهوا هذه البيوت عن المسجد فإني لا أحل المسجد لحائض ولا جنب
No references are available.
[Its chain is hasan] Bayhaqi narrated it, 2/442, 443, from the hadith of Abu Dawud through the same route, and Ibn Khuzayma graded it sahih, no. 1327, and the hadith has many corroborating witnesses.