Skip to content

Commit

Permalink
feat: add local cache for auth with NuxtHub admin
Browse files Browse the repository at this point in the history
Resolves #86
  • Loading branch information
Atinux committed Apr 25, 2024
1 parent 5296688 commit 27174a8
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/runtime/server/utils/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import type { H3Event } from 'h3'
import { getHeader, createError, handleCors } from 'h3'
import { $fetch } from 'ofetch'

let localCache: Record<string, boolean> = {}

Check failure on line 5 in src/runtime/server/utils/auth.ts

View workflow job for this annotation

GitHub Actions / lint

'localCache' is never reassigned. Use 'const' instead

Check failure on line 5 in src/runtime/server/utils/auth.ts

View workflow job for this annotation

GitHub Actions / test

'localCache' is never reassigned. Use 'const' instead

export async function requireNuxtHubAuthorization(event: H3Event) {
// Skip if in development
if (import.meta.dev) {
Expand Down Expand Up @@ -37,6 +39,9 @@ export async function requireNuxtHubAuthorization(event: H3Event) {

// Hosted on NuxtHub
if (projectKey) {
if (localCache[secretKeyOrUserToken]) {
return
}
// Here the secretKey is a user token
await $fetch(`/api/projects/${projectKey}`, {
baseURL: process.env.NUXT_HUB_URL || 'https://admin.hub.nuxt.com',
Expand All @@ -45,6 +50,7 @@ export async function requireNuxtHubAuthorization(event: H3Event) {
authorization: `Bearer ${secretKeyOrUserToken}`
}
})
localCache[secretKeyOrUserToken] = true
return
}

Expand Down

0 comments on commit 27174a8

Please sign in to comment.