7

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.

1
  • I need to ask some extra Q's related to a similiar query of mine. can I hv your email id? Thank you Commented Feb 24, 2021 at 10:46

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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?
I could not find Auth.js file or the directory you mentioned. Should directory and file be created?
0

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

you can see login for instance.

Comments

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 */
  },
};

1 Comment

hey, can you update this pls ? or i can contact you on discord?
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.

Comments

0

Yes it can be done on version 4.20.3. The identifier is your phone number.

enter image description here

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.