Created
January 28, 2022 02:54
-
-
Save crocker/60f256a5378fc2314e8f48247b1859d3 to your computer and use it in GitHub Desktop.
Auth0 Rule - Create/Update Stripe Customer on Auth0 Login
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
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