Facing the qiblah is one of the conditions of prayer being valid, and every worshipper should try to find out the direction of the qiblah when praying, either by using natural signs or compasses, if possible, or by asking trustworthy people in the place, who know the direction of the qiblah.
If a person prays then realizes that he was not facing the qiblah, if the deviation was slight then it does not matter and does not invalidate his prayer, because what everyone who is far away from the Ka'bah must do is face its general direction; it is not essential for him to face the Ka’bah itself, because of the report narrated by al-Tirmidhi (342) and Ibn Majaah (1011) from Abu Hurayrah (may Allaah be pleased with him) who said: The Messenger of Allaah (peace and blessings of Allaah be upon him) said: “Whatever is between the east and the west is qiblah.” Classed as saheeh by al-Albaani in Irwa’ al-Ghaleel (292).
Al-San’aani (may Allaah have mercy on him) said in Subul al-Salaam (1/260): This hadeeth indicates that it is obligatory to face the direction of qiblah, and not the exact position of the Ka’bah for those who are not able to face it precisely. End quote.
Shaykh Ibn ‘Uthaymeen (may Allaah have mercy on him) said: Thus we know that the matter is broad in scope, so if we see someone praying slightly off from the qiblah, that does not matter, because he is facing the general direction. End quote from al-Sharh al-Mumti’ (2/273).
But if the deviation from the direction of the Ka’bah is great, such that your prayer is not in the direction of the qiblah and the qiblah is behind him or to his right or left, then this is subject to further discussion:
1 – If a person has tried to work it out or find out, then he does not have to repeat the prayer, because he has done what was required of him, and Allaah says (interpretation of the meaning):
“So keep your duty to Allaah and fear Him as much as you can”
It says in Fataawa al-Lajnah al-Daa’imah (6/314): If a worshipper tries hard to work out the qiblah and he prays, then he finds out that his estimate was wrong, his prayer is still valid. End quote.
2 – But if he does not try hard to work it out or find out, then he has to repeat the prayer.
Shaykh Ibn ‘Uthaymeen said in al-Sharh al-Mumti’ (2/287): If he prays without trying to work it out and without following someone, if he gets it wrong he has to repeat the prayer, but if he gets it right he does not have to repeat it, according to the correct opinion. End quote.
“Following someone” means asking a trustworthy person about the direction of the qiblah and following what he says.
Based on that, if your deviation from the direction of the qiblah was severe, then you have to repeat the prayer.
And Allaah knows best.