It’s mostly not your fault. Apps, games, and socials are intentionally designed to be addictive. That’s because the more time you spend on them, the more ad revenue the owners make.
If sheer willpower doesn’t work, find other ways around it. Someone else already suggested going to a library or another place, that’s good advice. Of you phone is an issue, you could turn it off and allow yourself to turn it back on after you’ve been productive for an hour, or after you have achieved a specific goal.
Password managers. People will use anything but that: paper, notes app (without any security), using the same password everywhere…