RECEP TAYYIP Erdogan’s ugly win in Sunday’s referendum on a new, authoritarian constitution for Turkey creates big problems for the country’s secular democratic forces and for Turkey’s Western allies — but also for Mr. Erdogan himself. His victory was not convincing, as he had hoped, but narrow, contested and tainted by the finding of a European observer mission that the pre-election campaign was not free or fair. Turkey’s three biggest cities voted against the would-be strongman. The country is not united behind him, but polarized — a political reality that even an empowered ruler will ignore at his peril.

To be sure, the Turkish president sounded defiant in the wake of his victory, dismissing Western critics for their “crusader mentality” and hinting that he would embrace harsh new measures, such as reinstituting the death penalty — something that would surely rupture Turkey’s relations with European Union leaders. As it is, Mr. Erdogan’s government has purged some 130,000 people from their jobs and jailed more than 45,000 since a failed military coup last summer. The new constitution, which will take full effect in 2019, could allow him to remain president until at least 2029 , with only weak parliamentary checks and a judiciary he could shape with his own appointments.

Turkey, however, has not yet reached the state of Egypt or Russia, where elections are grossly rigged and most opposition has been crushed. Even Kurdish towns that have been assaulted by the military in the name of defeating terrorists turned out to vote against Mr. Erdogan, as did the large secular populations of Istanbul, Ankara and Izmir. Preliminary results showed 48.7 percent of the country voted against the constitution despite a one-sided campaign in which opposition voices were suppressed. A controversial decision by election authorities to accept ballots that lacked official stamps may have saved Mr. Erdogan from defeat, but at the price of further undermining his legitimacy.

Mr. Erdogan would be wise to try to defuse some of the opposition by reaching out to opponents, as Western governments urged him to do. Until 2015 he pursued a peace settlement with the militant Kurdistan Workers’ Party; some observers think he may return to it. But Mr. Erdogan’s history over 14 years in office has reflected an increasing hunger for power, matched by a growing intolerance of criticism. If that trend continues, Turkey will face relentless domestic strife.

All of this poses a dilemma for the United States and other NATO nations, which badly need Turkey as an anchor of the alliance on the borders of the Middle East but cannot easily countenance its drift toward dictatorship. The Trump administration awkwardly reflected this tension Monday as President Trump called Mr. Erdogan to offer congratulations and discuss Syria even as the State Department gingerly addressed the election irregularities and urged the government to “protect the fundamental rights and freedoms of all its citizens.” In the near term, Western leaders cannot afford to break with Mr. Erdogan, but they must do their best to push him toward ending his domestic repression. The millions of Turks who still seek to preserve democracy and civil liberties will need allies, too.