How can I get the user id after login and pass it to an api route?

Essentially what I am trying to do is pass the auth0 user ID to an API route in my application which calls a stripe endpoint which then creates a customer portal session. I need to pass the user ID to the api call but I am unable to call the useUser in an API route so I am unsure of how to proceed here.

const stripe = require('stripe')('some-api-key');

export default function create_customer_portal_session(req, res) {
    
    const  customerId = 'this should be the auth0 user id';
    const returnUrl = 'some-return-url';
    const portalSession = async () => await stripe.billingPortal.sessions.create({
        customer: customerId,
        return_url: returnUrl,
    });
    res.redirect(303, portalSession.url);
};

does anyone have an idea of how to do this?

Hey there @thekoala!

Considering this is post login, perhaps you could inspect the access token (or id token) of the user and use the sub claim (Auth0 user_id) in the code above - For reference, here’s an example access token.

Hope this helps!

2 Likes

I ended up figuring it out. All I had to do was create a form in one of my react components and pass the auth0 user id to it and since it was on the front end it had access to the useUser hook

<form method="POST" action="/api/create_customer_portal_session">
         <input type={'hidden'} name={'customerID'} value={customerID} onChange={() => {}}/>
         <button type="submit">Account Settings</button>
</form>

I had no idea I could pass a hidden value to an input. But, it worked out and everything is linked correctly now!

@thekoala Good stuff! Awesome to know you got working how you wanted and really appreciate you sharing with the community :smile:

This topic was automatically closed 15 days after the last reply. New replies are no longer allowed.