0

I have this function to add a custom claim when a user is verified his phone number

exports.phoneCustomClaim = functions.auth.user().onCreate((user) => {
    const sellerNumber = user.phone; // here i am not getting the phone number
    const uid = user.uid;
    const customClaims = {
      number: sellerNumber,
    };

    console.log(`Uid: ${uid}, number: ${sellerNumber}`);

    console.log(`before Seller number: ${sellerNumber}`);
    return admin.auth().setCustomUserClaims(uid, customClaims).then(() => {
        console.log(`claim added!`);
    })
    .catch(error => {
        console.log(error);
    })
});

Log

Uid: 1mlxF1T8xcWbByAzQ7rfKjdshj, number: undefined

please how to get that phone number?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Arup
  • 54
  • 1
  • 8

1 Answers1

0

I suspect that the user account is created (but not authenticated) before the phone number is actually verified. This is similar to how the flow for email verification works.

If that is the case, you'll have to work around the problem. As there currently is no trigger for Cloud Functions when a user profile is updated, the best you can do is call a callable Cloud Function from your code when the user has verified their phone number.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
  • functions.auth.user().onCreate() is triggered when the user is verified their phone number because you see that i am getting the UID but not the verified number. I thought that the way i get user.uid is there any way to get user.phone or any thing? – Arup Oct 17 '19 at 15:24
  • Are you absolutely sure that it's triggered on validation and not earlier? Delay the verification process for a few minutes, check the functions log, and find out for sure. – Doug Stevenson Oct 17 '19 at 16:06
  • I understood what you say but how the trigger fired before verified? because after the verification a new user is added in firebase auth , if it fired before verified then for every time when a user is try to verify phone number a new user is created in firebase auth before verified the user. – Arup Oct 18 '19 at 01:07
  • So if you call `signInWithPhoneNumber` and wait before confirming the code, the user doesn't (yet) show up in the Firebase console? – Frank van Puffelen Oct 18 '19 at 02:30
  • I haven't try this. let me try – Arup Oct 18 '19 at 02:46
  • @FrankvanPuffelen i have got user phone number using callable function thank you so much. – Arup Oct 18 '19 at 03:31
  • I have one more question, i know that after add a custom claim it need to refresh the token , @FrankvanPuffelen can you please help me how to do that ? means i want that after adding the claim it immediatly available for firebase security rules. – Arup Oct 18 '19 at 03:46
  • See https://stackoverflow.com/questions/52729144/firebase-custom-claims-doesnt-propagate/52732200#52732200, and https://stackoverflow.com/questions/54592255/angular-firebase-email-verification-false-after-verify/54594760#54594760 (which is about email verification, but you'll need the same solution) – Frank van Puffelen Oct 18 '19 at 04:01
  • https://stackoverflow.com/questions/58447913/firebase-permission-denied @FrankvanPuffelen please check it – Arup Oct 18 '19 at 09:28