6

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.

Tavinder Singh
  • 380
  • 1
  • 7
  • 17
  • I need to ask some extra Q's related to a similiar query of mine. can I hv your email id? Thank you – Speedy11 Feb 24 '21 at 10:46

4 Answers4

6

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.

Ghadban135
  • 465
  • 5
  • 8
  • How to implement this in Strapi v4? I did not find anything of how to modify auth controller in the docs. Can you please tell me how? I am trying to modify auth so that it would use Firebase auth so in front-end I am just verifying user by OTP and getting a JWT to be sent to strapi. How can I implement this? – 0x01Brain Oct 28 '22 at 12:44
  • I could not find Auth.js file or the directory you mentioned. Should directory and file be created? – 0x01Brain Oct 28 '22 at 12:47
0

I think you can add some change to auth.js
that file is on this address

you can see login for instance.

hamed hossani
  • 986
  • 2
  • 14
  • 33
0

@Ghadban125's answer is correct, though I'd like to add some more details.

Not only do you need to overwrite the callback function in ./node_modules/@strapi/plugin-users-permissions/server/controllers/auth.js. You'd also need to register your new function in your strapi-server.js (the one that you create under the src directory, not the one under node_modules, similar to how you overwrite the callback function) which looks like this:

const { callback } = require("./controllers/Auth.js");
const utils = require("@strapi/utils");
const { ApplicationError } = utils.errors;

module.exports = (plugin) => {
plugin.controllers.auth.callback = async (ctx) => {
    try {
      await callback(ctx);
      // ctx.send(result);
    } catch (error) {
      throw new ApplicationError(error.message);
    }
  };
}

You'll also need to differentiate the request's identifier between an email, username, or phone number. To do this, you'll need to edit your ./src/extensions/users-permissions/controllers/auth.js file:

/* left out for brevity */
const phoneNumberRegExp = /^[+]*[(]{0,1}[0-9]{1,4}[)]{0,1}[-\s\./0-9]*$/;
/* left out for brevity */
module.exports = {
  async callback(ctx) {
    /* left out for brevity */
    const query = { provider };

    // Check if the provided identifier is an email or not.
    const isEmail = emailRegExp.test(params.identifier);

    // Check if the provided identifier is a phone number or not.
    const isPhoneNumber = phoneNumberRegExp.test(params.identifier);

    // Set the identifier to the appropriate query field.
    if (isEmail) {
      query.email = params.identifier.toLowerCase();
    } else if (isPhoneNumber) {
      query.phoneNumber = params.identifier;
    } else {
      query.username = params.identifier;
    }
    /* left out for brevity */
  },
};
trphuongnam15
  • 61
  • 2
  • 4
0

I have implemented the same on this github repo - https://github.com/mayank-budhiraja/strapi-with-otp-integration

A user is authenticated only through the OTP verification and all auth requests are made using the JWT token.

MayankBudhiraja
  • 2,763
  • 2
  • 7
  • 11