From c69072ad5cd9375d48f50a3b6159cd307c0c54bc Mon Sep 17 00:00:00 2001 From: anteqkois Date: Sat, 8 Jun 2024 16:58:19 +0200 Subject: [PATCH] chore(webhooks): return data --- apps/api-gateway/src/main.ts | 3 +- .../components/LandingNavButtons.tsx | 1 - apps/web/app/(landing)/cors-error/page.tsx | 44 +++++++++++++++++++ apps/web/libs/api-client.ts | 9 ++++ .../billing/payments/stripe.service.ts | 8 ++++ .../modules/webhooks/webhooks.controller.ts | 35 ++++++++++++--- 6 files changed, 92 insertions(+), 8 deletions(-) create mode 100644 apps/web/app/(landing)/cors-error/page.tsx diff --git a/apps/api-gateway/src/main.ts b/apps/api-gateway/src/main.ts index 417a1901..afba00ac 100644 --- a/apps/api-gateway/src/main.ts +++ b/apps/api-gateway/src/main.ts @@ -27,9 +27,10 @@ async function bootstrap() { const configService = app.get(ConfigService) const frontendUrl = configService.getOrThrow('FRONTEND_HOST') + if (!frontendUrl.length) console.error('FRONTEND_HOST is empty') app.enableCors({ - origin: [frontendUrl], + origin: frontendUrl, methods: ['GET', 'POST', 'PUT', 'PATCH', 'DELETE'], credentials: true, }) diff --git a/apps/web/app/(landing)/components/LandingNavButtons.tsx b/apps/web/app/(landing)/components/LandingNavButtons.tsx index 1224d165..87f157d6 100644 --- a/apps/web/app/(landing)/components/LandingNavButtons.tsx +++ b/apps/web/app/(landing)/components/LandingNavButtons.tsx @@ -14,7 +14,6 @@ export function LandingNavButtons() { const [logoutButton, setLogoutButton] = useState(false) useEffect(() => { - console.log('authStatus', authStatus); if (authStatus === AuthStatus.AUTHENTICATED) setLogoutButton(true) else setLogoutButton(false) }, [authStatus]) diff --git a/apps/web/app/(landing)/cors-error/page.tsx b/apps/web/app/(landing)/cors-error/page.tsx new file mode 100644 index 00000000..4404020a --- /dev/null +++ b/apps/web/app/(landing)/cors-error/page.tsx @@ -0,0 +1,44 @@ +'use client' + +import { H4, Icons, P } from '@linkerry/ui-components/server' +import { useEffect } from 'react' +import { PageContainer } from '../../app/components/PageContainer' + +const clearCookies = () => { + document.cookie.split(';').forEach((c) => { + document.cookie = c.replace(/^ +/, '').replace(/=.*/, '=;expires=' + new Date().toUTCString() + ';path=/') + }) +} + +export default function IndexPage() { + useEffect(() => { + clearCookies() + }, []) + + return ( + +
+

+ + CORS error occures +

+

+ We encountered an issue with your request due to browser security policies (CORS), sometimes this can happen in browsers like Brave etc.. To + resolve this, please try the following steps: +

+
    +
  1. + 1. Logout: If you are logged in, please log out and then log back in. +
  2. +
  3. + 2. Clear Cookies: We have automatically cleared your cookies for this page. Please refresh the page. +
  4. +
  5. + 3. Turn Off Shields: If you are using the Brave browser, try turning off Shields for this site. +
  6. +
+

If the issue persists, please contact our support team for further assistance.

+
+
+ ) +} diff --git a/apps/web/libs/api-client.ts b/apps/web/libs/api-client.ts index 2b8c886c..c6f1f477 100644 --- a/apps/web/libs/api-client.ts +++ b/apps/web/libs/api-client.ts @@ -32,6 +32,15 @@ apiClient.interceptors.response.use( async (error) => { const originalRequest = error.config + if (!error.response) { + // Network error or CORS error + if (error.message === 'Network Error' || error.message.includes('CORS')) { + console.error('CORS error detected:', error.message) + window.location.href = '/cors-error' + return Promise.reject(error) + } + } + if (error?.response?.status == 401 && error?.config && !error?.config?._isRetry) { originalRequest._isRetry = true try { diff --git a/libs/nest-core/src/modules/billing/payments/stripe.service.ts b/libs/nest-core/src/modules/billing/payments/stripe.service.ts index d2af6223..7b161964 100644 --- a/libs/nest-core/src/modules/billing/payments/stripe.service.ts +++ b/libs/nest-core/src/modules/billing/payments/stripe.service.ts @@ -51,6 +51,10 @@ export class StripeService { status: object.status, }, } as SubscriptionUpdate) + + return { + success: true, + } } @StripeWebhookHandler('customer.subscription.updated') @@ -104,6 +108,10 @@ export class StripeService { id: object.metadata.subscriptionId, data: update, } as SubscriptionUpdate) + + return { + success: true, + } } private async _getCustomerOrCreate({ project }: { project: ProjectDocument<'owner'> }) { diff --git a/libs/nest-core/src/modules/webhooks/webhooks.controller.ts b/libs/nest-core/src/modules/webhooks/webhooks.controller.ts index 5c8e5f96..d3fde3f1 100644 --- a/libs/nest-core/src/modules/webhooks/webhooks.controller.ts +++ b/libs/nest-core/src/modules/webhooks/webhooks.controller.ts @@ -77,9 +77,17 @@ export class WebhooksController { return } // this._asyncHandler(payload, flow.toObject()).catch(exceptionHandler.handle) - this._asyncHandler(payload, flow.toObject()).catch((error) => { - this.logger.error(`#handleWebhookParams _asyncHandler failure`, ErrorCode.INTERNAL_SERVER, { error: error?.message }) - }) + try { + await this._asyncHandler(payload, flow.toObject()) + return response.send({ + success: true, + }) + } catch (error: any) { + this.logger.error(`#handleWebhookQuery _asyncHandler failure`, ErrorCode.INTERNAL_SERVER, { error: error?.message }) + return response.send({ + success: false, + }) + } } // @UseGuards(JwtBearerTokenAuthGuard) @@ -92,10 +100,19 @@ export class WebhooksController { if (isHandshake) { return } + // this._asyncHandler(payload, flow.toObject()).catch(exceptionHandler.handle) - this._asyncHandler(payload, flow.toObject()).catch((error) => { + try { + await this._asyncHandler(payload, flow.toObject()) + return response.send({ + success: true, + }) + } catch (error: any) { this.logger.error(`#handleWebhookQuery _asyncHandler failure`, ErrorCode.INTERNAL_SERVER, { error: error?.message }) - }) + return response.send({ + success: false, + }) + } } // @UseGuards(JwtBearerTokenAuthGuard) @@ -106,7 +123,9 @@ export class WebhooksController { const isHandshake = await this._handshakeHandler(flow.toObject(), payload, true, response) if (isHandshake) { - return + return { + success: true, + } } await this.webhooksService.simulationCallback({ @@ -118,6 +137,10 @@ export class WebhooksController { queryParams: request.query as Record, }, }) + + return response.send({ + success: true, + }) } private async _handshakeHandler(flow: FlowPopulated, payload: EventPayload, simulate: boolean, response: FastifyReply): Promise {