Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Add Stripe Customer Id to Auth0 via custom rule
function (user, context, callback) {
user.app_metadata = user.app_metadata || {};
if ('stripe_customer_id' in user.app_metadata) {
context.idToken['https://example.com/stripe_customer_id'] = user.app_metadata.stripe_customer_id;
return callback(null, user, context);
}
var stripe = require('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['https://example.com/stripe_customer_id'] = user.app_metadata.stripe_customer_id;
callback(null, user, context);
})
.catch(function(err) {
callback(err);
});
});
}
@calcifer1337

This comment has been minimized.

Copy link

@calcifer1337 calcifer1337 commented Aug 20, 2019

Hey thanks this is very helpful.

@prateek2408

This comment has been minimized.

Copy link

@prateek2408 prateek2408 commented May 18, 2020

Hey, very useful. Works like a charm

@horelvis

This comment has been minimized.

Copy link

@horelvis horelvis commented Sep 17, 2020

Thank you! 👏

@YO-SC

This comment has been minimized.

Copy link

@YO-SC YO-SC commented Dec 8, 2020

What kind of info would replace example.com and stripe_customer_id (self explanatory, but I want to be sure) on the following line of code @vktr?

context.idToken['https://example.com/stripe_customer_id']

@vktr

This comment has been minimized.

Copy link
Owner Author

@vktr vktr commented Dec 8, 2020

@YO-SC it is just used for namespacing so it can be anything, but I usually namespace my own claims under a domain I own.

@YO-SC

This comment has been minimized.

Copy link

@YO-SC YO-SC commented Dec 8, 2020

@vktr Alright, I also forgot asking about this line:

var stripe = require('stripe')('sk_....');

What goes under the sk_...., is this a placeholder or is it used for namespacing as well ?

(Sorry for all the questions, I am fairly new to web development; at least in these type of scenarios)

@vktr

This comment has been minimized.

Copy link
Owner Author

@vktr vktr commented Dec 8, 2020

@YO-SC that's where you put your Stripe secret key

@YO-SC

This comment has been minimized.

Copy link

@YO-SC YO-SC commented Dec 10, 2020

@vktr Thanks for all the replies and support 👼 👍 😄

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment