Skip to content

Commit

Permalink
feat(auth): forward cf clearance cookie to api via auth proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
ygrishajev committed Jan 16, 2025
1 parent cc8cdaf commit bd81f2f
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { LoggerService } from "@akashnetwork/logging";
import type { Context } from "hono";
import { singleton } from "tsyringe";

Expand All @@ -8,6 +9,8 @@ import { StripeWebhookService } from "@src/billing/services/stripe-webhook/strip

@singleton()
export class CheckoutController {
private readonly logger = LoggerService.forContext(CheckoutController.name);

constructor(
private readonly authService: AuthService,
private readonly checkoutService: CheckoutService,
Expand Down Expand Up @@ -35,6 +38,9 @@ export class CheckoutController {
if (error.message === "Price invalid") {
return c.redirect(`${redirectUrl}?invalid-price=true`);
}

this.logger.error(error);

return c.redirect(`${redirectUrl}?unknown-error=true`);
}
}
Expand Down
12 changes: 7 additions & 5 deletions apps/deploy-web/src/pages/api/proxy/[...path].ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { getSession } from "@auth0/nextjs-auth0";
import httpProxy from "http-proxy";
import type { NextApiHandler } from "next";

import { serverEnvConfig } from "@src/config/server-env.config";

export default async (req, res) => {
// removes the api prefix from url
req.url = req.url.replace(/^\/api\/proxy/, "");
const proxyToApi: NextApiHandler = async (req, res) => {
req.url = req.url?.replace(/^\/api\/proxy/, "");

console.log("proxy:", req.url);
const session = await getSession(req, res);

// don't forward the cookies to the target server
req.headers.cookie = "";
const cfClearanceCookie = req.cookies["cf_clearance"];
req.headers.cookie = cfClearanceCookie ? `cf_clearance=${cfClearanceCookie}` : "";

if (session?.accessToken) {
req.headers.authorization = `Bearer ${session.accessToken}`;
Expand Down Expand Up @@ -41,3 +41,5 @@ export const config = {
bodyParser: false
}
};

export default proxyToApi;

0 comments on commit bd81f2f

Please sign in to comment.