Skip to content

Commit

Permalink
feat: api route proxy
Browse files Browse the repository at this point in the history
  • Loading branch information
etiennecl committed Mar 11, 2024
1 parent 9098e1d commit 1e330e0
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 8 deletions.
22 changes: 22 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
API_URL=http://localhost:7999

# Enable or disable authentication if disabled it will mock the user and session
AUTH_ENABLE=false
# This is the client ID of this app
AUTH_CLIENT_ID=
# This is the client secret of this app
AUTH_CLIENT_SECRET=
# This is the URL of the auth server
AUTH_ISSUER_URL=https://auth.dev.clinia.com
# This is the secret used to sign the session cookie
AUTH_SECRET=
# This is the URL to redirect to after logout
AUTH_POST_LOGOUT_REDIRECT_URI=https://auth.dev.clinia.com/logout



# Instrumentation
OTEL_EXPORTER_OTLP_ENDPOINT=http://localhost:55679
OTEL_EXPORTER_OTLP_INSECURE=true
OTEL_EXPORTER_OTLP_PROTOCOL=grpc
NEXT_OTEL_VERBOSE=1
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,7 @@ yarn-error.log*
# typescript
*.tsbuildinfo
next-env.d.ts

.env
.env.*
!.env.example
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"test": "vitest"
},
"dependencies": {
"@auth0/nextjs-auth0": "^3.5.0",
"@clinia-ui/icons": "^0.1.8",
"@clinia-ui/react": "^0.1.8",
"@clinia/search-sdk-core": "^0.1.0",
Expand All @@ -32,6 +33,7 @@
"devDependencies": {
"@testing-library/react": "^14.2.1",
"@trivago/prettier-plugin-sort-imports": "^4.3.0",
"@types/http-proxy": "^1.17.14",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
Expand Down
106 changes: 106 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 2 additions & 8 deletions src/pages/api/[...path].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSession, withApiAuthRequired } from '@/lib/auth';
import { withApiAuthRequired } from '@/lib/auth';
import { logger } from '@/logger';
import httpProxy from 'http-proxy';
import type { NextApiRequest, NextApiResponse } from 'next';
Expand All @@ -14,22 +14,16 @@ export const config = {
// We are currently using this proxy to avoid CORS issues with the API.
// We are using api routes from the page directory as it is easier to have a catch all route and using a reverse proxy.
async function proxy(req: NextApiRequest, res: NextApiResponse) {
const session = await getSession(req, res);
return new Promise<void>((resolve, reject) => {
if (req.url) {
req.url = req.url.replace('/api', '');
}

// Set the authorization header to the idToken from the session
if (session?.idToken) {
req.headers.authorization = `Bearer ${session.idToken}`;
}

proxyServer.web(
req,
res,
{
target: process.env.API_URL ?? 'http://localhost:3000',
target: process.env.API_URL ?? 'http://localhost:7999',
},
(err: Error | null | undefined) => {
if (err) {
Expand Down

0 comments on commit 1e330e0

Please sign in to comment.