Obama may have been the best candidate in both elections, but that's not why the people elected him.
in 2008, the majority of people elected the non-Republican party because of the Bush presidency.
In 2012, Obama was re-elected (in my opinion) because he was a standing president who was not doing a bad job.
The true shame of the 2012 election was that the only consistent political point that all Republican candidate brought to the table was "beat Obama no matter what." That's hardly a strategy for leading a country.