either or all:
# check the battery - settings app -> battery — under battery health -> Peak performance capability - it should say that battery is supporting normal peak performance. If not — a battery needs replacement.
# some short inside or water damage and thus corrosion or some in-between so that if power usage exceeds the base levels - the battery does not cope and / or short occures and the system shuts down.
# loose connector
The phone may need to be opened to evaluate 2. and 3.