How to set context.requestLanguage for a hook

Following on from the closed question at Verification Email missing user_metadata, I am trying to send out a localised verification email.

It is clear from the previous question and our own experiments, that the user_metadata is not preset and available when crafting the verification email. We have also tried to set it in a rule, but again it is clear that this rule is run too late and the metadata is not available when sending the email.

We have however, been able to use a prehook to hardcode the locale on the user_metadata but as the previous poster mentioned the request query is not available at this point.

The obvious field to use would be the requestLanguage field as shown when editing a prehook,
@param {string} context.requestLanguage - language of the client agent
However this is empty for us and we have not been able to find any information about how to set it.

Can someone please point me in the right direction? I believe that if we can set this context field our flow will work and we will be able to send users emails in the correct language.

Many thanks!
Rachael

Hi @rachael.huber,

Thanks for joining us in the Auth0 Community!

I am taking a look at this and have some comments/questions.

I think the context.requestLanguage should be coming from the Accept-language header.

What happens when you try to use the object? undefined?

Let me know,
Dan

Hi Dan.

This code in a pre user registration hook:

module.exports = function (user, context, cb) {
  var response = {};

  response.user = user;

  // Add user or app metadata to the newly created user
  response.user.user_metadata = { language: 'de', requestlangage: context.requestLanguage, locale: context.locale, score: 7 };
  // response.user.app_metadata = { vip: true, score: 7 };

  cb(null, response);
};

gives me this result:

{
  "language": "de",
  "score": 7
}

Thanks a lot for taking a look.

@rachael.huber,

I tested your hook and get this user_metadata:

{
  "language": "de",
  "requestlangage": "en-US,en;q=0.9",
  "score": 7
}

I don’t see context.locale as available in a pre-reg hook.

Can you dm me the name of your tenant so I can take a look at your setup?

Thanks,
Dan

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.