Skip to content

Instantly share code, notes, and snippets.

@solace
Forked from vktr/rule.js
Last active May 7, 2022 15:48
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save solace/64e67582d761bc93e70044fd42e3fa44 to your computer and use it in GitHub Desktop.
Save solace/64e67582d761bc93e70044fd42e3fa44 to your computer and use it in GitHub Desktop.
Auth0 Rule: Add Stripe Customer Id to existing token data
// If you have multiple rules in your workflow that need to update token data.
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
const token_namespace = 'https://your-domain.com/app_metadata';
// assumes your metadata object is shallow
const addCustomerId = stripe_customer_id => ({
...context.idToken[token_namespace],
stripe_customer_id
});
if ('stripe_customer_id' in user.app_metadata) {
context.idToken[token_namespace] = addCustomerId(user.app_metadata.stripe_customer_id);
return callback(null, user, context);
}
var stripe = require('stripe')(STRIPE_SK);
var customer = {
email: user.email
};
stripe.customers.create(customer, function(err, customer) {
if (err) {
return callback(err);
}
user.app_metadata.stripe_customer_id = customer.id;
auth0.users.updateAppMetadata(user.user_id, user.app_metadata)
.then(function() {
context.idToken[token_namespace] = addCustomerId(user.app_metadata.stripe_customer_id);
callback(null, user, context);
})
.catch(function(err) {
callback(err);
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment