It is true that, according to the US Constitution, the way a state's electors are chosen is determined by the state legislature. Most states currently give the winner of the statewide vote all the electors of the state (technically, they allow the winning party choose the slate of electors), but two states currently give the winner of the statewide vote the 2 electors, and give the winner in each congressional district's vote 1 elector. But there is no requirement for there to be a popular vote or votes to determine the electors at all. Historically, many states have at one point had the state legislature choose the electors directly.
I think there is generally no dispute that if a state legislature were to change the manner of choosing electors back to themselves (the state legislature) choosing the electors, before a presidential election, that that would be legal and constitutional. However, your example is a little different -- in your example, the state legislature would have previously determined that the electors be chosen by some kind of popular election on Election Day, and then after the election happened, but before the electors meet, the state legislature changes the manner of selecting electors. That has never happened in any state so far, so there is no precedent on whether this is constitutional or not.
There was one time when such a thing was considered, however. In the 2000 presidential election, where the winner of Florida was disputed for a long time with a series of slow recounts, there was consideration by the Florida legislature (then controlled by Republicans) to simply select the Republican slate of electors directly rather than wait for the results of the recounts. At the time, George W. Bush (the Republican presidential candidate) still had a lead. Ultimately, this was not needed as the US Supreme Court stopped the recounts and Bush was declared to have won Florida. There are differing opinions on whether such a step would have been constitutional or not if they had carried it out.
The Florida case differs somewhat from the example you mentioned. First, in the Florida case, the winner was unclear rather than one side having definitely won. However, if one party would go to the extraordinary lengths to have a state legislature choose the winner, then I bet that they would have already done everything possible, legally as well as in terms of public opinion, to (rightly or wrongly) challenge, dispute, and stall the determination of the results of the election in their state, so that it may appear to the public as disputed anyway. And second, in the Florida case, Bush was still leading in Florida at the time the legislature was considering this, so that it was somewhat reasonable to pick the winner as Bush. But if in a hypothetical example, the Democratic candidate were to be officially leading in the state, then if the legislature were to pick the Republican candidate as the winner, that would be a lot harder to justify. And again, whether this would be constitutional has never been tested.
Remember though, that Congress has the power to reject electoral votes from a state when counting it, or to choose between slates of electors if multiple slates sent votes to Congress. But to reject electors would require a majority of both houses of Congress, so it depends on the makeup of Congress to determine whether partisanship could stand in the way of rejection. And rejection would only reduce the electoral votes from the candidate the state legislature decided on; it cannot give electoral votes to the right candidate, so even if Congress can reject, it would not entirely fix the problem.