Skip to content

Commit

Permalink
feat: configure cors for apiClient for different domains usecase
Browse files Browse the repository at this point in the history
  • Loading branch information
NamesMT committed Jun 6, 2024
1 parent c72c07f commit b8288c0
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 5 deletions.
2 changes: 2 additions & 0 deletions apps/backend/.env
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# FRONTEND_URL=https://YOUR_FRONTEND_URL

# KINDE_DOMAIN=https://<YOUR_SUBDOMAIN>.kinde.com
# KINDE_CLIENT_ID=<YOUR_CLIENT_ID>
# KINDE_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
Expand Down
8 changes: 8 additions & 0 deletions apps/backend/.env.local
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
NODE_ENV=development
APP_DEV_host=127.0.0.1
APP_DEV_port=3301

FRONTEND_URL=https://127.0.0.1:3300

# KINDE_DOMAIN=https://<YOUR_SUBDOMAIN>.kinde.com
# KINDE_CLIENT_ID=<YOUR_CLIENT_ID>
# KINDE_CLIENT_SECRET=<YOUR_CLIENT_SECRET>
# KINDE_REDIRECT_URI=http://localhost:3000/callback
# KINDE_LOGOUT_REDIRECT_URI=https://<YOUR_SUBDOMAIN>.kinde.com/logged-out
6 changes: 4 additions & 2 deletions apps/backend/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { handle } from '@namesmt/hono-adapter-aws-lambda'
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { isDevelopment } from 'std-env'
import { env, isDevelopment } from 'std-env'

import type { HonoEnv } from './types'
import { devAdapter, tryServeApp } from './dev'
Expand All @@ -15,7 +15,9 @@ if (isDevelopment)

export const app = _app
// CORS middleware
.use(cors())
.use(cors({
origin: ['127.0.0.1', new URL(env.FRONTEND_URL!).hostname],
}))

// Session management middleware
.use(sessionMiddleware())
Expand Down
8 changes: 5 additions & 3 deletions apps/frontend/app/plugins/rpcApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ export default defineNuxtPlugin({
const callProxy = enableProxy === 'auto'
? urlBackend.hostname === url.hostname
: enableProxy
const apiClient = hc<typeof app>(callProxy
? `https://${url.host}`
: backendUrl,
const apiClient = hc<typeof app>(
callProxy
? `https://${url.host}`
: backendUrl,
{ init: { credentials: 'include' } },
)

return {
Expand Down

0 comments on commit b8288c0

Please sign in to comment.