Search code examples
authenticationstrapi

Phone Number authentication in Strapi


I am using Strapi for my android app and I need to login user by their phone number. There are many auth providers like email and password, google, facebook etc. But I can not find any documentation about adding phone number authentication. Please help.


Solution

  • This is possible to do that. You will have to use the customization concept to customize the callback function of the users-permissions plugin.

    First, you should define phone_number field inside the User model.

    Then, you should overwrite extensions/users-permissions/controllers/Auth.js by add query.phone_number = params.identifier; under const query = { provider };

     const query = { provider };    
          // Check if the provided identifier is an email or not.  
     const isEmail = emailRegExp.test(params.identifier);  
          // Set the identifier to the appropriate query field.  
          if (isEmail) {  
            query.email = params.identifier.toLowerCase();  
          } else {  
            query.phone_number = params.identifier;  
          }
    

    In this example, we tell Strapi that we can login by entering an email or phone number both are accepted.

    And you can remove the if-condition and just write query.phone_number = params.identifier; if you want to login with a phone number only.