When Hillary Clinton delivered a campaign post-mortem to her major supporters in a telephone conference call late last week, she blamed her loss in the presidential election on FBI Director James Comey. She should have blamed the loss on herself. Her refusal to safeguard state secrets while she was secretary of state and her failure to grasp the nationwide resentment toward government by the forgotten folks in the middle class were far likelier the cause of her defeat than was Comey.

Yet it is obvious that law enforcement-based decisions in the past four months were made with an eye on Election Day, and the officials who made them evaded the rule of law.

Here is the back story.

The statutory obligation of the FBI is to gather evidence to aid in the prosecution or prevention of federal crimes or breaches of national security. The process of complying with this obligation necessarily involves making some legal judgments about the relevance, probity and even lawfulness of the gathered evidence. These judgments are sometimes made on the streets in an emergency and sometimes made after consultation and consensus. But the whole purpose of this evidence-gathering and decision-making is to present a package to the Department of Justice, for which the FBI works, for its determination about whether or not to seek a prosecution.

In cases in which subpoenas are needed, the FBI must work in tandem with the DOJ because subpoenas in criminal cases can be issued only by grand juries and only DOJ lawyers can ask grand juries to issue them. Usually, the FBI and the DOJ work together to present what they have to a grand jury in order to build a case for indictment or to induce a grand jury to issue subpoenas and help them gather more evidence.

Federal judges become involved when search warrants or arrest warrants are needed. These are often emergent situations, as the evidence to be seized or the person to be arrested might be gone if not pursued in short order. They require the presentation of evidence to a judge quickly and in secret. It is the judge’s role to decide whether the DOJ/FBI team has met the constitutional threshold of probable cause. Probable cause is met when the prosecutorial team shows the judge that the evidence the team seeks from the execution of the warrant more likely than not will implicate someone in criminal behavior.

Having issued many search and arrest warrants myself, I know that judges need to be curious and skeptical. After all, only one side is appearing before the judge, and the whole appearance is often quick, unorthodox and in secret. A healthy curiosity and skepticism will cause a prudent jurist to ask whether the grand jury really needs what the search warrant seeks. If the reply is that there is no grand jury, most judges will terminate the application and conclude that it is a fishing expedition -- or going “sideways,” as law enforcement says -- not a serious criminal investigation worthy of judicial involvement.

All of this is commanded by law to be kept secret so as to preserve evidence, avoid tipping off a potential defendant capable of flight and preserve the reputation of a person not indicted.

That is at least the way these things are supposed to work. Yet none of this happened in the recently reopened and re-terminated investigation of the misuse of emails containing state secrets by Clinton.

In that investigation, the DOJ did not present evidence to a grand jury. Thus, it did not obtain any subpoenas. And it did not seek any search warrants. It cut deals left and right, promising not to prosecute those from whom it sought problematic evidence. After accumulating a mountain of evidence of Clinton’s guilt, the FBI did not present it to the DOJ.

Rather, Director Comey held a news conference on July 5, at which he declared that he and his colleagues in the FBI -- not the DOJ -- had concluded that “no reasonable prosecutor” would take the case; so Clinton would not be prosecuted. He then proceeded to outline in detail the gathered evidence against Clinton.

He endured a firestorm of criticism for his public presentation of the gravity of the evidence in the case and his unilateral determination of no prosecution. The firestorm was generated largely by his own FBI agents who had become convinced of Clinton’s guilt of the failure to safeguard state secrets (espionage), as well as their collective belief that someone somewhere had told Comey what to do.

Then, just 11 days before the 2016 presidential election, Comey saw a chance to redeem himself with his critics. He unlawfully announced the unexpected discovery of a treasure-trove of 650,000 emails that he and his team had not then examined but that they thought might affect the decision not to prosecute. This caused a second firestorm, in which this writer and others accused Comey of profound violations of federal law, not the least of which was an assault on Clinton’s right to due process.

Knowing this announcement -- not the resumption of the investigation but the announcement of it -- was unlawful, Attorney General Loretta Lynch did nothing to prevent it.

Clinton began to sink in the polls. Her Republican opponent, Donald Trump, now the president-elect, began to gloat and celebrate. Then, two days before the election, Comey announced that the FBI had reviewed all 650,000 recently discovered emails in a week and concluded that none of them affected the decision not to prosecute Clinton. Shortly thereafter, a DOJ official announced that the email investigation was closed -- for a second time.

What have we here?

We have the gross mismanagement of the nation’s premier law enforcement agency. We have a DOJ uninterested in the truth and willing to shield a target of a criminal investigation for political reasons. We have the improper and unlawful revelation of matters the law quite properly commands be kept secret.

We have the dangerous injection of the FBI into elective politics, which can do ruinous harm to the rule of law.

And we had a candidate who should blame only herself for the whole controversy.

Andrew P. Napolitano, a former judge of the Superior Court of New Jersey, is the senior judicial analyst at Fox News Channel.