Skip to content

Commit

Permalink
refactor(open-customer-portal): remove redundant error handling (#123)
Browse files Browse the repository at this point in the history
  • Loading branch information
twlite authored Oct 30, 2024
1 parent 6201425 commit d7acedd
Showing 1 changed file with 12 additions and 18 deletions.
30 changes: 12 additions & 18 deletions frontend/actions/open-customer-portal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,20 @@ export type responseAction = {
const billingUrl = absoluteUrl('/');

export async function openCustomerPortal(userStripeId: string): Promise<responseAction> {
let redirectUrl: string = '';
const session = await auth();

try {
const session = await auth();

if (!session?.user || !session?.user.email) {
throw new Error('Unauthorized');
}
if (!session?.user?.email) {
throw new Error('Unauthorized');
}

if (userStripeId) {
const stripeSession = await stripe.billingPortal.sessions.create({
customer: userStripeId,
return_url: billingUrl,
});
if (userStripeId) {
const stripeSession = await stripe.billingPortal.sessions.create({
customer: userStripeId,
return_url: billingUrl,
});

redirectUrl = stripeSession.url as string;
}
} catch (error) {
throw new Error('Failed to generate user stripe session');
redirect(stripeSession.url as string);
}

redirect(redirectUrl);
throw new Error('Failed to generate user stripe session');
}

0 comments on commit d7acedd

Please sign in to comment.