I'm creating a mobile app that will have a verification process of the phone number like most of apps do when they use your phone.
I'm also developing the back-end but I don't know exactly what is the safer way to generate a 4-6 digits code and send by SMS to verify that account.
How should I implement the generation of the code? And also, how to validate if it's a valid code for that phone?
Should I generate a random code and store it in the database? How to ensure the code is unique for each phone? Is it necessary to handle the case when I have more than 999999 (6 digits number max) users requesting the code even when it's certain that it won't happen?