Skip to content

Commit 27174a8

Browse files
committed
feat: add local cache for auth with NuxtHub admin
Resolves #86
1 parent 5296688 commit 27174a8

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

src/runtime/server/utils/auth.ts

+6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { H3Event } from 'h3'
22
import { getHeader, createError, handleCors } from 'h3'
33
import { $fetch } from 'ofetch'
44

5+
let localCache: Record<string, boolean> = {}
6+
57
export async function requireNuxtHubAuthorization(event: H3Event) {
68
// Skip if in development
79
if (import.meta.dev) {
@@ -37,6 +39,9 @@ export async function requireNuxtHubAuthorization(event: H3Event) {
3739

3840
// Hosted on NuxtHub
3941
if (projectKey) {
42+
if (localCache[secretKeyOrUserToken]) {
43+
return
44+
}
4045
// Here the secretKey is a user token
4146
await $fetch(`/api/projects/${projectKey}`, {
4247
baseURL: process.env.NUXT_HUB_URL || 'https://admin.hub.nuxt.com',
@@ -45,6 +50,7 @@ export async function requireNuxtHubAuthorization(event: H3Event) {
4550
authorization: `Bearer ${secretKeyOrUserToken}`
4651
}
4752
})
53+
localCache[secretKeyOrUserToken] = true
4854
return
4955
}
5056

0 commit comments

Comments
 (0)