KindeServerSession values are null when using Stripe webhook

Hi, I am using NextJS and want to integrate with the Stripe API. I need to use the Stripe's webhook for billing process but as this webhook is called from Stripe URL I cannot retrieve kindeServerSession values to validate the user.
export async function POST(request: Request) {
const {getUser, isAuthenticated} = getKindeServerSession();
const body = await request.text();
const signature = headers().get("Stripe-Signature") ?? "";
const user = await getUser() // 'user' is null here
const kindeSession = await isAuthenticated() // 'kindeSession' is false here but it should be true

//...some code
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string,
);
await saveUserSubscription({
subscriptionId: subscription.id,
stripeCustomerId: subscription.customer as string,
productId: process.env.BASIC_PRODUCT_ID ?? '',
currentPeriodEnd: new Date(
subscription.current_period_end * 1000,
).toString(),
}, user.id)
}
}
export async function POST(request: Request) {
const {getUser, isAuthenticated} = getKindeServerSession();
const body = await request.text();
const signature = headers().get("Stripe-Signature") ?? "";
const user = await getUser() // 'user' is null here
const kindeSession = await isAuthenticated() // 'kindeSession' is false here but it should be true

//...some code
const subscription = await stripe.subscriptions.retrieve(
session.subscription as string,
);
await saveUserSubscription({
subscriptionId: subscription.id,
stripeCustomerId: subscription.customer as string,
productId: process.env.BASIC_PRODUCT_ID ?? '',
currentPeriodEnd: new Date(
subscription.current_period_end * 1000,
).toString(),
}, user.id)
}
}
I am using the billingportal and I cannot pass metadata to the request
const stripeSession = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId,
return_url: 'http://localhost:3000/dashboard',
}, {
apiKey: '',
});

return { url: stripeSession.url };
const stripeSession = await stripe.billingPortal.sessions.create({
customer: stripeCustomerId,
return_url: 'http://localhost:3000/dashboard',
}, {
apiKey: '',
});

return { url: stripeSession.url };
Is any workaround for this?
1 Reply
Daniel_Kinde
Daniel_Kinde8mo ago
Hi, Is it the stripe webhook your not seeing the authenticated state?
Want results from more Discord servers?
Add your server