Said Sheikh Muhammad bin Ibrahim (Mufti of Saudi Kingdom before Sheikh Bin Baz, died in 1389 H):
This toilet is inseparable from 2 possibilities:
First: Separated from the mosque with a wall separated from the wall of the mosque located in the direction of Qiblah, there is no prohibition and no prayer in it, even though the toilet is in the direction of the mosque, as long as the building is separated from the mosque wall.
Second: Connected with the mosque, and there is no barrier except the wall of the mosque which is in the direction of Qiblah, it is mentioned by the scholars that this includes a makruh place of prayer facing it and not enough just the walls of the mosque because the salaf rahimahumullahu hate salat in the mosque in the direction kiblatnya There is a place of defecation, therefore it should separate the toilets from the wall of the mosque with a wall separated from the walls of the mosque. (Fatawa Wa Rasail Sheikh Muhammad bin Ibrahim no: 515)