iPhone 5 shutting down automatically again and again
Since your phone is 3 years old and operates correctly when plugged in, it's time for battery replacement.  Doesn't matter that you think the battery is at 50 or 80 percent, the battery has aged and the indicator is no longer giving you accurate information a result.