Invitation email template

I am trying to use user.app_metadata.someprop in User Invitation email template but none of user.app_metadata fields are getting populated. Not even user.picture. Only user.email works so far. I was expecting to be able to use any common variables. Customize Email Templates

Hi @lucas.majewski ,

Welcome to the Auth0 Community!

We are looking into this topic and will provide updates to you soon.

1 Like

tested with the following scripts in the body of the email template (verify email using link), and it’s working fine.

<h1>please setup your password {{user.app_metadata.color}}</h1>
              <h1>country is {{ user.email }}, {{ user.user_metadata.country }} </h1>
              
              <img 
                   src= {{ user.picture}}
                   width ="50"
                   alt="your picture"
                   />
1 Like

Hi @lucas.majewski ,

I tested this issue with the “Verification email with link” template and it works fine for me.

Here are my scripts:

<h1> {{user.app_metadata.color}}</h1>
<h1>country is {{ user.user_metadata.country }} </h1>
<img 
                   src= {{ user.picture}}
                   width ="50"
                   alt="your picture"/>

I tried to trigger the “user invitation” template in Management API with below scripts, it says successful, but email is not received for some reason.
{
“connection_id”:“xxx”,
“client_id”:“xxx”,
“email”: “xxx”,
“includeEmailInRedirect”: false
}

Ideally, if the scripts works for the “Verification email with Link” template, it should work for the “User Invitation” template as well.

Could you share with us your scripts in the body of the email template?

1 Like

Ultimately this script took care of:

  1. onExecutePostUserRegistration
  2. Add user to an org
  3. Generate password change ticket
  4. Send org invite with the change password ticket link via {{user.app_metadata.must_reset_password_link}}

/**

  • Handler that will be called during the execution of a PostUserRegistration flow.
  • @param {Event} event - Details about the context and user that has registered.
    */
    var ManagementClient = require(‘auth0’).ManagementClient;

const logPrefix = "WelcomeAndChangePassword: ";

exports.onExecutePostUserRegistration = async (event) => {
const userId = event.user.user_id;
console.log(logPrefix, userId, “starting”);

const originating_organization_id = event.user.app_metadata.originating_organization_id;
if (!originating_organization_id)
throw new Error(‘Missing ‘user.app_metadata.originating_organization_id’’);

const originating_client_id = event.user.app_metadata.originating_client_id;
if (!originating_client_id)
throw new Error(‘Missing ‘user.app_metadata.originating_client_id’’);

const auth0MgmtClient = new ManagementClient({
domain: event.tenant.id + ‘.us.auth0.com’,
clientId: event.secrets.auth0MgmtClientId,
clientSecret: event.secrets.auth0MgmtClientSecret,
scope: ‘create:organization_members create:organization_invitations create:user_tickets’,
});

console.log(logPrefix, userId, “adding user to organization”, originating_organization_id);

await auth0MgmtClient.organizations.addMembers({
id: originating_organization_id
}, {
members: [userId]
});

console.log(logPrefix, userId, “creating password change ticket”, originating_organization_id, originating_client_id);

const loginUrl = event.user.app_metadata.login_url;

console.log(logPrefix, userId, “login_url from from user.app_metadata (not used)”, loginUrl);

let passwordChangeTicketResult = await auth0MgmtClient.createPasswordChangeTicket({
//result_url: loginUrl,
organization_id: originating_organization_id,
client_id: originating_client_id,
user_id: userId,
includeEmailInRedirect: true,
mark_email_as_verified: true
});

console.log(logPrefix, userId, “creating organization invitation”, originating_organization_id, originating_client_id);

await auth0MgmtClient.organizations.createInvitation({
id: originating_organization_id
}, {
send_invitation_email: true,
client_id: originating_client_id,
invitee: {
email: event.user.email
},
inviter: {
name: “Inviter”
},
app_metadata: {
must_reset_password_link: passwordChangeTicketResult.ticket
}
});

console.log(logPrefix, userId, “finished”);
};

1 Like

Glad to know that you have figured out the solution. Thank you for sharing your scripts with us! :clap:

1 Like