-
-
Save solace/64e67582d761bc93e70044fd42e3fa44 to your computer and use it in GitHub Desktop.
Auth0 Rule: Add Stripe Customer Id to existing token data
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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