I’m trying to build a post login auth0 flow that will check if the user has an active subscription within stripe and if yes, create an app_metadata value of active and if not, set it to in-active — can someone help me with that
Hey there @lance1 welcome to the community!
While I haven’t set this up myself, I’m assuming you’ll need to hit a specific Stripe API to check for an active subscription and then set metadata accordingly? If so, an example action might look like:
exports.onExecutePostLogin = async (event, api) => {
const axios = require('axios');
try {
// Replace with Stripe API endpoint
const apiEndpoint = 'https://stripe-api-endpoint.com/data';
// Perform the GET request
const response = await axios.get(apiEndpoint);
// Check some condition in the response to set your flag
// This is a placeholder condition. Adjust it to suit your actual logic.
const flagValue = response.data.someCondition === true;
// Update the user's app_metadata
api.user.setAppMetadata('yourFlag', flagValue);
} catch (error) {
// Handle any errors
console.error('Error calling external API:', error);
// You might also want to set the flag to a default value in case of error
api.user.setAppMetadata('yourFlag', false);
}
};
Thank you, although it’s not working
I can’t figure out the details around the specific stripe api names and fields etc
No problem, happy to help where I can! I unfortunately have not worked with the Stripe API myself so won’t be of much help when it comes to specifics
@lance1 check out the example action code in this tutorial: https://developer.auth0.com/resources/labs/actions/sync-stripe-customers-and-auth0-users#create-a-post-login-action
It creates a new customer in Stripe if they don’t exist, but you can skip that part and just set app_metadata instead. The example uses Stripe SDK rather than calling specific endpoints.
Woo! Thanks @thameera
I was able to figure it out, here is the code.
It pulls the subscription list from Stripe and checks it for an active subscription
If active, it creates an app_metadata record called “stripe_membership_status” = true
If not-active, it creates an app_metadata record called “stripe_membership_status” = false
const stripe = require(‘stripe’);
let stripeClient = null;
const isCustomerActive = async (id, stripeSecret) => {
stripeClient ||= new stripe(stripeSecret);
const stripeResponse = await stripeClient.subscriptions.list({ customer: id })
const subscriptions = stripeResponse.data
return subscriptions.some((s) => s.status === ‘active’)
};
exports.onExecutePostLogin = async (event, api) => {
const CUSTOMER_KEY = event.user.app_metadata.stripe_customer_id
const STRIPE_SECRET_KEY = event.secrets.STRIPE_SECRET_KEY
const metadata = Object.entries(event.user.app_metadata);
const customerId = event.secrets.CUSTOMER_KEY
const isActive = await isCustomerActive(customerId, STRIPE_SECRET_KEY)
metadata.push([‘active’, isActive])
if (event.authorization) {
for (const [k, v] of metadata) {
api.idToken.setCustomClaim(k, v);
api.accessToken.setCustomClaim(k, v);
api.user.setAppMetadata(“stripe_membership_status”, v);
}
}
};
Thanks for sharing @lance1 !
To implement a post-login Auth0 flow that checks a user’s subscription status in Stripe:
- Set up Stripe webhook notifications.
- Create a backend endpoint to handle Stripe events and update Auth0 metadata.
- After login, call the endpoint to check subscription status.
- Update user metadata based on the status.
Regards,
James