-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: make direct api call opt-in for the saas
- Loading branch information
1 parent
5ccec8f
commit d1f9d08
Showing
5 changed files
with
143 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import { clerkClient, getAuth } from "@clerk/nextjs/server"; | ||
import { NextApiRequest, NextApiResponse } from "next"; | ||
import httpProxyMiddleware from "next-http-proxy-middleware"; | ||
|
||
const API_URL = process.env.BACKEND_URL; | ||
const isCanary = process.env.NEXT_PUBLIC_APP_DEPLOYMENT === "CANARY_CHECKER"; | ||
const env = process.env.ENV; | ||
const isClerkAuth = process.env.NEXT_PUBLIC_AUTH_IS_CLERK === "true"; | ||
|
||
const canaryPrefix = isCanary ? "" : "/canary"; | ||
|
||
export const config = { | ||
api: { | ||
bodyParser: false | ||
} | ||
}; | ||
|
||
async function getTargetURL(req: NextApiRequest) { | ||
if (isClerkAuth) { | ||
const user = getAuth(req); | ||
const org = await clerkClient.organizations.getOrganization({ | ||
organizationId: user.sessionClaims?.org_id! | ||
}); | ||
const backendURL = org?.publicMetadata?.backend_url; | ||
// for now, lets fallback to the old way of doing things, if the backend_url | ||
// is not set in the org metadata | ||
const target = backendURL; | ||
return target; | ||
} | ||
return API_URL; | ||
} | ||
|
||
const clerkBackendPathRewrites = [ | ||
{ | ||
patternStr: "^/api", | ||
replaceStr: "/" | ||
} | ||
]; | ||
|
||
const kratosBackendPathRewrites = ["localhost", "netlify"].includes(env!) | ||
? [ | ||
{ | ||
patternStr: "^/api", | ||
replaceStr: "/api" | ||
} | ||
] | ||
: [ | ||
{ | ||
patternStr: "^/api/canary", | ||
replaceStr: `${canaryPrefix}` | ||
}, | ||
{ | ||
patternStr: "^/api/.ory", | ||
replaceStr: "/kratos/" | ||
}, | ||
{ | ||
patternStr: "^/api", | ||
replaceStr: "/" | ||
} | ||
]; | ||
|
||
export default async function handler( | ||
req: NextApiRequest, | ||
res: NextApiResponse | ||
) { | ||
console.log("Proxying request to backend"); | ||
const target = await getTargetURL(req); | ||
|
||
if (!target) { | ||
return res.status(500).json({ error: "Missing target" }); | ||
} | ||
|
||
console.log(`Proxying to ${target}`); | ||
|
||
return httpProxyMiddleware(req, res, { | ||
target: target!, | ||
xfwd: true, | ||
pathRewrite: [ | ||
...(isClerkAuth ? clerkBackendPathRewrites : kratosBackendPathRewrites) | ||
] | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters