According to documentation it’s possible to localize email verification message sent to the newly registered users. Due to the bug reported here Access user language user_metadata.lang in email template the language value is empty.
Is there any progress on this? Is there any workaround?
I’m not actually sure this is a bug, but rather a confusing doc. This is saying you could use a variable stored in user_metadata to configure language, not that there is one stored automatically. Here is the doc that states that you could store language preference in user_metadata.
This obviously isn’t clear and I will put a request in to update the doc.
As far as a solution goes, how do you want to determine language? From the Accept-Language header, or from user-selected input in your app or login page?
I was able to set up a rule that grabs the ui_locales and sets it as the user_metadat.lang, but at this point in the pipeline the email template has already used the older user_metadata object with no language, so it won’t affect any emails going out on the first login cycle.
Are you users database users? Or do you have social/enterprise users as well?
I am going to post the rule as it could help for other emails (like password reset).
function(user, context, callback){
// set metadata object
user.user_metadata = user.user_metadata || {};
// if lang exists for the user, skip rule
if(user.user_metadata.lang){
callback(null, user, context);
}
// set language variable to ui_locales or default to 'en'
let preferedLanguage = context.request.query.ui_locales || 'en';
// update the user_metadata that will be part of the response
user.user_metadata.lang = preferedLanguage;
// persist the user_metadata update
auth0.users.updateUserMetadata(user.user_id, user.user_metadata)
.then(function(){
callback(null, user, context);
})
.catch(function(err){
callback(err);
});
}
This actually works indeed for the 2nd and subsequent mails sent, that’s a progress
Is there some other way to localize the first verification email sent to users? Seems that’s the missing piece to implement full localization of emails.
If you were only using DB users you could get away with doing this in a hook. Sounds like that isn’t the case, so you will need another solution. Let me look into it further and I will report back.
But as far as I understand the verification email is sent only to users who sign up with their email. There’s no verification email for apple/google/fb.
Sure, you will want to create a pre-registration hook like this:
/**
@param {object} user - The user being created
@param {string} user.tenant - Auth0 tenant name
@param {string} user.username - user name
@param {string} user.password - user's password
@param {string} user.email - email
@param {boolean} user.emailVerified - is e-mail verified?
@param {string} user.phoneNumber - phone number
@param {boolean} user.phoneNumberVerified - is phone number verified?
@param {object} context - Auth0 connection and other context info
@param {string} context.renderlanguage - language used by signup flow
@param {string} context.request.ip - ip address
@param {string} context.request.language - language of the client agent
@param {object} context.connection - information about the Auth0 connection
@param {object} context.connection.id - connection id
@param {object} context.connection.name - connection name
@param {object} context.connection.tenant - connection tenant
@param {object} context.webtask - webtask context
@param {function} cb - function (error, response)
*/
module.exports = function (user, context, cb) {
var response = {};
response.user = user;
response.user.user_metadata = { lang: context.renderLanguage };
cb(null, response);
};
The ui_locales param isn’t available directly in the hook. Instead, we use the renderLanguage of the login page. Whatever language the Universal Login page is rendered in will be set as user_metadata.lang.
BTW is there a way to localize the webpage displayed after the e-mail address is confirmed? I mean the “Your email address was successfully verified” message? I’ve not found settings for this in web console.