Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Auth0 rule for sending emails of signed up users to Mailchimp.
/*
* Update a Mailchimp list with user emails during the signup or the first login.
*
* Create the following atributes in the Auth0 RULE configuration settings.
* https://auth0.com/docs/rules/guides/configuration
*
* MC_TOKEN: Mailchimp API Token. E.g.: ef235a44355dda3e61ea074ae0d439a2-us20
* MC_LIST_ID: ID of the Mailchimp list you would like to populate. E.g.: ca9eb2555e
* MC_API_URL: Mailchimp API URL. E.g.: https://us20.api.mailchimp.com/3.0
*
**/
function (user, context, callback) {
// Short-circuit if the user signed up already
if (context.stats.loginsCount > 1 || context.protocol === 'oauth2-refresh-token') {
return callback(null, user, context);
}
const request = require('request');
const ctx = {
email_address: user.email,
merge_fields : {"FNAME": user.given_name || "",
"LNAME": user.family_name || ""},
status: "subscribed"
};
var auth = "Basic " + new Buffer("token:" + configuration.MC_TOKEN).toString("base64");
request.post({
url: configuration.MC_API_URL + "/lists/" + configuration.MC_LIST_ID + "/members",
headers : {"Authorization" : auth},
json: ctx
});
// Don’t wait for the Mailchimp call to finish, return right away (the request will continue on the sandbox)`
callback(null, user, context);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment