Fortunately, hackers are known to be an existing threat, so you can prepare your Apple ID account page for the challenge. First, use a very strong password that is hard to remember. Ideally, it's a long combination of letters, numbers, and special characters. Then turn on 2FA so that trusted devices can receive verification codes for all important actions (such as resetting passwords).
On Mac, open System Preferences ➙ Apple ID
Go to the Password and Security tab
Click Turn on next to two-factor authentication
Finally, be extra careful about all the official-looking emails you get, such as the ones from Apple. Always click on the sender to make sure you see the actual email the message is being sent from. Moreover, instead of clicking links in the email, go to the iforgot.apple.com unlock feature directly from your browser.