There are several things you can do if you have to sleep near your fire for warmth.
Set a long fire thet emits heat along the length of your body.
Use a reflector, preferably of green logs, along the back of the fire to reflect heat towards you.
Arrange your shelter to trap and circulate the heat (but not the smoke) from the fire.
Use wood that burns with a long, hot flame and doesn't throw sparks.
Using these methods should mean you can sleep far enough from the fire to be out of the reach of any sparks that are thrown toward you.
Generally though, you should only be using a fire for warmth while sleeping if you have no proper sleeping kit. Fire retardent materials aren't especially good at letting heat in which would make it pointless setting a fire before turning in for the night.
If you do find yourself needing external heat during the night you can warm the ground first with your fire, then move the fire, let the ground cool slightly then sleep on the warm ground.
This is a much more efficient way of using the heat from the fire for keeping warm at night, although it takes a bit of preperation.