Skip to content

Instantly share code, notes, and snippets.

@crocker
Created January 28, 2022 02:54
Show Gist options
  • Save crocker/60f256a5378fc2314e8f48247b1859d3 to your computer and use it in GitHub Desktop.
Save crocker/60f256a5378fc2314e8f48247b1859d3 to your computer and use it in GitHub Desktop.
Auth0 Rule - Create/Update Stripe Customer on Auth0 Login
function (user, context, callback) {
var stripe = require('stripe')('sk_test....');
user.app_metadata = user.app_metadata || {};
var customer = {
email: user.email,
name: user.name
};
if ('stripe_customer_id' in user.app_metadata) {
context.idToken['https://blah.com/stripe_customer_id'] = user.app_metadata.stripe_customer_id;
stripe.customers.update(
user.app_metadata.stripe_customer_id,
customer
);
return callback(null, user, context);
}
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['https://blah.com/stripe_customer_id'] = 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