Germany wins WW1. Mauretania and Morocco, in it's pre 1903 border, become the german protectorate of Deutsch-Marokko. Which means Tindouf and Bechar are part of Morocco. The Rif is also part of the protectorate because the treaty of Fes between Morocco, Spain and France was declared void. But Germany allowed Spain to remain in the Sahara.
The germans invest heavily into infrastructure and mining and also build some naval bases along the coasts. They also build a rail line through spanish Sahara to connect Morocco to Mauretania.
The spanish republic wins the civil war by 1940, but it needs every penny to rebuild and bring it's economy back on track.
Spain puts tariffs on the german companies and trains using it's saharan territory. At first the germans accept. Later, in 1944, Spain, led by a leftist government, grows closer to the Soviets and raises the tariffs. The germans blocade Spain's saharan and rifian ports and place german and moroccan troops on the border. After two weeks the spanish governer is killed by locals and disguised moroccan troops take over Layoun. With no support the spanish troops in the Sahara surrender. The same scenario takes place in the north. The spanish government is forced to resign and the new government accepts to hand over officialy the colonies and enclaves to Morocco and Germany. By 1955 Morocco becomes independent and a very close german ally.