No it was not legal, it clearly states in the 10th Amendment "The powers not delegated to the United States by the Constitution, nor prohibited by it to the States, are reserved to the States respectively, or to the people." Meaning that they could secede if the state wanted to since the federal goverment wasn't reserved that power.