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.
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"
/>
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?
Ultimately this script took care of:
- onExecutePostUserRegistration
- Add user to an org
- Generate password change ticket
- 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â);
};
Glad to know that you have figured out the solution. Thank you for sharing your scripts with us!