Setting and accessing user.user_metadata in passwordless email templates

Hello,

I am trying to set up localization for a Passwordless verification code email in order to have English and Spanish translated emails.

We currently have the Universal Login page localization working and correctly setting the ui_locales parameter for English/Spanish based on user input.

I have written a Pre User Registration hook as detailed in this thread

module.exports = function(user, context, cb) {
  var response = {};
  response.user = user;
  response.user.user_metadata = { lang_code: context.renderLanguage };
  cb(null, response);

I tested out the hook in the debug tool and confirmed that it was setting the user.user_metadata.lang_code from the context.renderLanguage.

Here is the test data. Note: the context.renderLanguage is set to "es"

{
  "user": {
    "id": "abc123",
    "tenant": "my-tenant",
    "username": "user1",
    "password": "xxxxxxx",
    "email": "user1@foo.com",
    "emailVerified": false,
    "phoneNumber": "1-000-000-0000",
    "phoneNumberVerified": false,
    "user_metadata": {
      "hobby": "surfing"
    },
    "app_metadata": {
      "plan": "full"
    }
  },
  "context": {
    "renderLanguage": "es",
    "request": {
      "language": "en-us",
      "ip": "127.0.0.1"
    },
    "connection": {
      "id": "con_xxxxxxxxxxxxxxxx",
      "name": "Username-Password-Authentication",
      "tenant": "my-tenant"
    }
  }
}

Here is the test response. Note: the user.user_metadata.lang_code has the value "es" as expected.

{
  "statusCode": 200,
  "headers": {},
  "status": "success",
  "data": {
    "user": {
      "id": "abc123",
      "tenant": "my-tenant",
      "username": "user1",
      "password": "xxxxxxx",
      "email": "whocares",
      "emailVerified": false,
      "phoneNumber": "1-000-000-0000",
      "phoneNumberVerified": false,
      "user_metadata": {
        "lang_code": "es"
      },
      "app_metadata": {
        "plan": "full"
      }
    }
  }
}

I inserted the hook into my Pre User Registration flow:

In my Passwordless email template, I have the following liquid syntax that should show the localized translation. Note: I have included the {% debug %} tag

{% assign langCode = user.user_metadata.lang_code %}
{% if langCode  == 'en' %}
    <p style="font-size: 2em; line-height: 1.9; color: #00597B">English Translation registration code: </p>
{% elsif langCode  == 'es' %}
    <p style="font-size: 2em; line-height: 1.9; color: #00597B">Spanish Translation registration code: </p>
{% endif %}
<p style="font-size: 2em; line-height: 1.9; color: #00597B"><b>{{ code }}</b></p>
{% debug %}

When the verification code email comes through, the user object that is displayed in the debug output only contains the “email” property:

"user": {
    "email": "cdefoe@redacted.com"
  }

This behavior seems to align with what is observed in this thread as well as the Passwordless emails section of the auth0 customize email templates documentation here:

Since this appears to be intended behavior, and the customize actions auth0 docs indicate that metadata cannot be added in a pre-user-registration Action for Passwordless users:

I am left with the following questions:

  • Will there be support added at some point for Passwordless Email template to be able to access properties on the user object other than user.email? For example, user.user_metadata.lang_code
  • Is there some other workaround that can be used to pass in a language code that can be used to localize the message in my Passwordless Email template?

Thank you,
Chris

2 Likes

Hi there!
We’re making an effort to address our backlog and I wanted to update this topic with a suggestion for a workaround:

You could work around this problem by using a Post-Login Action script. Because users are logged in immediately after signing up, this script will always trigger.

exports.onExecutePostLogin = async (event, api) => {
  if (event.connection.name === "email"){
    api.user.setUserMetadata("x", "y ")
  } 
  else {
    api.user.setUserMetadata("x1", "y1 ")
  }
};

Thanks!