I had the exact same problem! Phone would boot into the rom, or stock and freeze after about a minute and reboot itself constantly. I had the updated version of TWRP and was flashing the new 1/30 Mahdi build and it happened after I did that! I tried flashing stock roms, aosp roms, and eventually did a Flash back to stock method, which non of them worked. I took the phone into T-Mobile and did a warranty replacement, got a new one overnight. Not sure if it is related to Mahdi or not, but it happened after a supposed successful flash.
I tried my TWRP Backups too, and those still restarted on me. Nothing helped me..Tried wiping the sd card, format the whole card, wipe cache, fix perm..
Hopefully you have a warranty or figure it out!
Good luck.