Skip to content

Commit

Permalink
fix(proxy): read env from request
Browse files Browse the repository at this point in the history
  • Loading branch information
atinux committed Sep 24, 2024
1 parent 785df95 commit bd4de45
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/runtime/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,21 @@ export async function requireNuxtHubAuthorization(event: H3Event) {
return
}

// Check if authorization header is set
const secretKeyOrUserToken = (getHeader(event, 'authorization') || '').split(' ')[1]
if (!secretKeyOrUserToken) {
throw createError({
statusCode: 403,
message: 'Missing Authorization header'
})
}
const projectKey = process.env.NUXT_HUB_PROJECT_KEY

// Get project key and secret key
const env = event.context.cloudflare?.env || process.env || {}
const projectKey = env.NUXT_HUB_PROJECT_KEY

// Self-hosted NuxtHub project, user has to set a secret key to access the proxy
const projectSecretKey = process.env.NUXT_HUB_PROJECT_SECRET_KEY
const projectSecretKey = env.NUXT_HUB_PROJECT_SECRET_KEY
if (projectSecretKey && secretKeyOrUserToken === projectSecretKey) {
return
} else if (projectSecretKey && !projectKey) {
Expand All @@ -37,7 +41,7 @@ export async function requireNuxtHubAuthorization(event: H3Event) {
}
// Here the secretKey is a user token
await $fetch(`/api/projects/${projectKey}`, {
baseURL: process.env.NUXT_HUB_URL || 'https://admin.hub.nuxt.com',
baseURL: env.NUXT_HUB_URL || 'https://admin.hub.nuxt.com',
method: 'HEAD',
headers: {
authorization: `Bearer ${secretKeyOrUserToken}`
Expand Down

0 comments on commit bd4de45

Please sign in to comment.