I am building a token-based authentication API, and I have three main methods:
Register Method: It takes credentials (email, password, phone number) and creates the user, then sends an OTP message to the phone number provided in the credentials input. At this stage, I haven't implemented any login, session or token creation; I'm simply creating the user.
Verify: This method takes a phone number and a code. It checks the OTP code, and if it is correct, it will get the user data associated with that phone number from db and update its database column 'verify_phone_at' to 'verified' in order to mark the user as verified. Additionally, it will create the token. (here i created the token and verified the user depending on the input phone number)
Login Method: Ensures that the user is verified and creates a new token.
resendVerifyCode: It takes a phone number as credentials and resends the OTP message to that phone number.
The security concerns here are:
1- Verify Method: The user inputs his phone number, and a message is sent to that phone. It's possible that the phone number entered may be different from the one associated with the user's account in register stage because I don't have access to user data (haven't logged in yet to access the authenticated user). The only constraint is that the phone number must exist and be attached to an account, a user can input his friend's phone number, verify it, obtain a token, and access their friend's account without entering email or password just from verify the account.
How to solve this problem: Should I create session for the user after the creation account stage and only generate the token when the user verified his account?