Is the patient’s prayer valid if he takes narcotic medicine?
Question

Is it permissible for a patient who takes narcotic medicine to pray?
Praise be to Allah.Firstly:
The basic principle is that it is not permissible to take narcotic medicines or drugs, especially if their effect is powerful and has a clear impact on the patient’s consciousness and understanding.
But if it is necessary, and the doctors have decided that the patient should take this narcotic medicine, and there is no non-narcotic alternative, then it is permissible for him to take it, subject to certain conditions.
It was stated in the Eighth Medical Fiqh Conference: An Islamic View of some Medical Problems – which was held in Kuwait –:
Narcotic substances are prohibited, and it is not permissible to take them except for the purpose of specific medical treatment, in the amounts prescribed by the doctors, if they are pure (tahir) in and of themselves."(Al-Fiqh al-Islami wa Adillatuhu, Wahbah az-Zuhayli 7/5266).
For more information, please see the answer to question no. 192321 .
Secondly:
With regard to the validity of the prayer of someone who takes a narcotic medicine at the time when he is taking it, there are two scenarios:
The first scenario is where he becomes unconscious when taking it, or he does not fully know what he is saying or doing. In this case his prayer is not valid until he comes back to full consciousness and awareness, because Allah, may He be exalted, says (interpretation of the meaning): {O you who have believed, do not approach prayer while you are intoxicated until you know what you are saying} [An-Nisa’ 4:43].
The second scenario is where he knows fully what he is saying and doing when he takes the narcotic medicine. In this case, his prayer is valid. The scholars have stated that the prayer of one who is intoxicated is valid if he is aware of what he is saying, so that is more likely to be applicable in this case.
Ash-Shafa‘i (may Allah have mercy on him) said: If someone who has drunk something haram prays and is not intoxicated, he is sinning for having drunk that haram thing, but he does not have to repeat his prayer."(Al-Umm  1/88).
Shaykh ‘Abd al-‘Aziz ibn Baz (may Allah have mercy on him) was asked about the one who prays after having drunk alcohol when he is aware of what he is saying. He replied: His prayer is valid if he prayed when he was sober and the drunkenness had left him; he may do wudu’ and pray. But if he is in a state of intoxication, then his prayer is not valid because he is not aware of what he is saying."(Fatawa Nur ‘ala ad-Darb by Ibn Baz  6/25).
Shaykh al-Islam Ibn Taymiyah stated that the prayer of one who has consumed alcohol or hashish is valid if he prays when he knows what he is saying.
See: Majmu‘ al-Fatawa (33/103; 23/358).
And Allah knows best.