Is there a way to get user email in smsDelivery function? I'm using 2fa (thirdpartyemailpassword + p...
f
Is there a way to get user email in smsDelivery function? I'm using 2fa (thirdpartyemailpassword + paswordless) recipe
This is my override of smsDelivery:
Copy code
private overridePasswordlessSmsDelivery(
    originalImplementation: SmsDeliveryInterface<TypePasswordlessSmsDeliveryInput>,
    notificationService: NotificationService,
  ): SmsDeliveryInterface<TypePasswordlessSmsDeliveryInput> {
    return {
      ...originalImplementation,
      async sendSms(
        input: TypePasswordlessSmsDeliveryInput & {
          userContext: any;
        },
      ) {
        if (input.type === 'PASSWORDLESS_LOGIN') {
          return notificationService.sendOTPCode(
            input.phoneNumber,
            input.userInputCode,
          );
        }

        return originalImplementation.sendSms(input);
      },
    };
  }
As you can see I have phone number but I'm not sure how to get email. I tried passing it via usercontext but it didn't work.
r
hey @fdundjer
you have the user ID as the input as well
using which you can query to get the email
f
Hm, I can't see it in input
Is it part of usercontext?
r
oh right. Ok. I misunderstood.
You have the phone number, using which you can get the user ID (passwordless.getUserByPhoneNumber(...) function)
or something like that
and using the user ID, you can then get the email
f
That should do it, I'll give it a try, thanks!
r
that
that's if the user exists in the passwordless recipe.
If the user doesn't exist, then you will get
undefined
Since this is the second factor, and a session exists already when this API is called, you can pass in the email of the user from the session's user ID via userContext
and then read the userContext in this function to get the email
f
Hm, I'm not sure that I follow. How can I get the email from session itself instead of fetching user by phone number, then querying metadata to find first factor user which has passwordless_user_id set to the one from phone number?
r
the session has session.getUserId() which will give you the user id of the first session
then you can use thirdpartyemailpassword.getUserbyId(), in which you will get the email
f
But I don't see how can I access session? smsDelivery doesn't have request object
I tried putting it in usercontext but I don't receive it in smsDelivery
r
you will need to do this in the API that creates the code.
You will anyway be overriding that API as per the customisation docs (i think)
f
Hm, I'll give it a try. I did this first time in createCodePOST
r
yea. Im not sure what you mean by first time, but in that API.
after you call getSession
f
I already tried implementing this, and I was not receiving data that I set in usercontext. I'll give it a try again and post outcome.
Thanks
r
ok cool. If you don't get the data propogated in user context, AND if the sendSms function is called after you have set the user context in the API, please let me know.
f
It's working. I probably made a mistake doing it first time. Thanks @rp_st