Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 968
Tavinder Singh Avatar asked Jun 20 '19 09:06

Tavinder Singh


1 Answers

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

  • Customization concept - https://strapi.io/documentation/v3.x/concepts/customization.html#plugin-extensions
  • Function to update - https://github.com/strapi/strapi/blob/master/packages/strapi-plugin-users-permissions/controllers/Auth.js#L21
  • For example:

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.

like image 83
Ghadban135 Avatar answered Nov 15 '22 12:11

Ghadban135