From a2e68f320cc6c3bdc36c011a678d3d76270641b0 Mon Sep 17 00:00:00 2001 From: Valentyne Stigloher Date: Sat, 15 Mar 2025 16:30:26 +0100 Subject: [PATCH 1/2] (fix) another usage of useRuntimeConfig outside of Nitro --- server/cards.ts | 1 + server/initTestDatabase.ts | 2 +- server/jwt-cli.ts | 5 +++-- server/jwt.ts | 19 ++++++++++++++----- src/domain.ts | 12 ++++++++---- 5 files changed, 27 insertions(+), 12 deletions(-) diff --git a/server/cards.ts b/server/cards.ts index 8354d648e..28731a49d 100644 --- a/server/cards.ts +++ b/server/cards.ts @@ -66,6 +66,7 @@ const shoot = async (db: Database, mode: 'light' | 'dark'): Promise => { authenticated: true, }, '15m', + process.env.NUXT_PUBLIC_DOMAIN_BASE ?? '', ); pr.source(`${urlBases[locale] + username}?token=${encodeURIComponent(token)}`, ['1024x300'], { diff --git a/server/initTestDatabase.ts b/server/initTestDatabase.ts index 9fd9aa64b..29b47c8f4 100644 --- a/server/initTestDatabase.ts +++ b/server/initTestDatabase.ts @@ -66,7 +66,7 @@ async function initTestDatabase(): Promise { authenticated: true, }; - const token = await jwt.sign('_', adminUser, '3650d'); + const token = await jwt.sign('_', adminUser, '3650d', process.env.NUXT_PUBLIC_DOMAIN_BASE ?? ''); await db.close(); return token; diff --git a/server/jwt-cli.ts b/server/jwt-cli.ts index b1d218aed..0b2f38917 100644 --- a/server/jwt-cli.ts +++ b/server/jwt-cli.ts @@ -27,13 +27,14 @@ import jwt from './jwt.ts'; process.stdin.on('end', () => resolve(data)); }); + const domainBase = process.env.NUXT_PUBLIC_DOMAIN_BASE ?? ''; switch (operation) { case 'encrypt': - console.log(await jwt.sign('_', JSON.parse(input))); + console.log(await jwt.sign('_', JSON.parse(input), '365d', domainBase)); break; case 'decrypt': - console.log(JSON.stringify(await jwt.validate('_', input), null, 2)); + console.log(JSON.stringify(await jwt.validate('_', input, domainBase), null, 2)); break; default: diff --git a/server/jwt.ts b/server/jwt.ts index f8332a389..6dc7f36f7 100644 --- a/server/jwt.ts +++ b/server/jwt.ts @@ -21,17 +21,26 @@ class Jwt { return new Jwt(privateKey, publicKey); } - async sign(locale: string, payload: JWTPayload, expiresIn = '365d'): Promise { + async sign( + locale: string, + payload: JWTPayload, + expiresIn = '365d', + domainBase: string | undefined = undefined, + ): Promise { return await new SignJWT(payload) .setProtectedHeader({ alg: 'RS256' }) .setExpirationTime(expiresIn) - .setAudience(getUrlsForAllLocales(locale)) - .setIssuer(getUrlForLocale(locale)) + .setAudience(getUrlsForAllLocales(locale, false, domainBase)) + .setIssuer(getUrlForLocale(locale, domainBase)) .sign(this.privateKey); } - async validate(locale: string, token: string): Promise { - const urls = getUrlsForAllLocales(locale); + async validate( + locale: string, + token: string, + domainBase: string | undefined = undefined, + ): Promise { + const urls = getUrlsForAllLocales(locale, false, domainBase); try { const { payload } = await jwtVerify(token, this.publicKey, { algorithms: ['RS256'], diff --git a/src/domain.ts b/src/domain.ts index 1fcf3a2a8..fa8870da8 100644 --- a/src/domain.ts +++ b/src/domain.ts @@ -22,13 +22,17 @@ export const getLocaleUrls = (domainBase: string | undefined): Record { - return getLocaleUrls(useRuntimeConfig().public.domainBase)[locale]; +export const getUrlForLocale = (locale: string, domainBase: string | undefined = undefined) => { + return getLocaleUrls(domainBase ?? useRuntimeConfig().public.domainBase)[locale]; }; -export const getUrlsForAllLocales = (locale: string, includeUnpublished: boolean = false) => { +export const getUrlsForAllLocales = ( + locale: string, + includeUnpublished: boolean = false, + domainBase: string | undefined = undefined, +) => { const locales = buildLocaleList(locale, includeUnpublished); - const urls = getLocaleUrls(useRuntimeConfig().public.domainBase); + const urls = getLocaleUrls(domainBase ?? useRuntimeConfig().public.domainBase); const codes = ['_', ...Object.values(locales).map((localeDescription) => localeDescription.code)]; return codes.map((code) => urls[code]); From b05289522171c3caad9cf13174c0d9b77a11e874 Mon Sep 17 00:00:00 2001 From: Valentyne Stigloher Date: Sat, 15 Mar 2025 16:31:06 +0100 Subject: [PATCH 2/2] (nuxt) update commands in package.json --- package.json | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index f376a361c..a40287289 100644 --- a/package.json +++ b/package.json @@ -7,11 +7,9 @@ "scripts": { "prepare-dev": "nuxi prepare && pnpm run-file locale/generateSchemas.ts", "dev": "nuxi dev", - "build": "NODE_OPTIONS=\"--max-old-space-size=5120\" nuxt build", - "start": "nuxt start", - "export": "nuxt export", - "serve": "nuxt serve", + "build": "NODE_OPTIONS=\"--max-old-space-size=5120\" nuxi build", "run-file": "node --import=./server/ts-node-esm.js --env-file=.env", + "typecheck": "nuxi typecheck", "lint": "eslint . --fix", "test": "vitest" },