diff --git a/eslint.config.js b/eslint.config.js index a351fa9..807a144 100644 --- a/eslint.config.js +++ b/eslint.config.js @@ -28,6 +28,6 @@ export default [ } }, { - ignores: ['build/', '.svelte-kit/', 'dist/'] + ignores: ['build/', '.svelte-kit/', 'dist/', '.wrangler'] } ]; diff --git a/package-lock.json b/package-lock.json index 36980d8..0fc1422 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,7 +26,7 @@ "prettier-plugin-svelte": "^3.1.2", "stripe": "^16.7.0", "svelte": "^5.0.0-next.244", - "svelte-check": "^3.6.0", + "svelte-check": "^4.0.1", "svelte-stripe": "^1.2.0", "tslib": "^2.4.1", "typescript": "^5.0.0", @@ -1951,13 +1951,6 @@ "@types/node": "*" } }, - "node_modules/@types/pug": { - "version": "2.0.10", - "resolved": "https://registry.npmjs.org/@types/pug/-/pug-2.0.10.tgz", - "integrity": "sha512-Sk/uYFOBAB7mb74XcpizmH0KOR2Pv3D2Hmrh1Dmy5BmK3MpdSa5kqZcg6EKBdklU0bFXX9gCfzvpnyUehrPIuA==", - "dev": true, - "license": "MIT" - }, "node_modules/@typescript-eslint/eslint-plugin": { "version": "8.4.0", "resolved": "https://registry.npmjs.org/@typescript-eslint/eslint-plugin/-/eslint-plugin-8.4.0.tgz", @@ -2478,16 +2471,6 @@ "node": ">=8" } }, - "node_modules/buffer-crc32": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/buffer-crc32/-/buffer-crc32-1.0.0.tgz", - "integrity": "sha512-Db1SbgBS/fg/392AblrMJk97KggmvYhr4pB5ZIMTWtaivCPMWLkmb7m21cJvpvgK+J3nsU2CmmixNBZx4vFj/w==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=8.0.0" - } - }, "node_modules/cac": { "version": "6.7.14", "resolved": "https://registry.npmjs.org/cac/-/cac-6.7.14.tgz", @@ -2803,16 +2786,6 @@ "node": ">=6" } }, - "node_modules/detect-indent": { - "version": "6.1.0", - "resolved": "https://registry.npmjs.org/detect-indent/-/detect-indent-6.1.0.tgz", - "integrity": "sha512-reYkTUJAZb9gUuZ2RvVCNhVHdg62RHnJ7WJl8ftMi4diZ6NWlciOzQN88pUhSELEwflJht4oQDv0F0BMlwaYtA==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=8" - } - }, "node_modules/devalue": { "version": "5.0.0", "resolved": "https://registry.npmjs.org/devalue/-/devalue-5.0.0.tgz", @@ -2850,13 +2823,6 @@ "node": ">= 0.4" } }, - "node_modules/es6-promise": { - "version": "3.3.1", - "resolved": "https://registry.npmjs.org/es6-promise/-/es6-promise-3.3.1.tgz", - "integrity": "sha512-SOp9Phqvqn7jtEUxPWdWfWoLmyt2VaJ6MpvP9Comy1MceMXqE6bxvaTu4iaxpYYPzhny28Lc+M87/c2cPK6lDg==", - "dev": true, - "license": "MIT" - }, "node_modules/esbuild": { "version": "0.21.5", "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.21.5.tgz", @@ -3384,13 +3350,6 @@ "dev": true, "license": "ISC" }, - "node_modules/fs.realpath": { - "version": "1.0.0", - "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", - "integrity": "sha512-OO0pH2lK6a0hZnAdau5ItzHPI6pUlvI7jMVnxUQRtw4owF2wk8lOSabtGDCTP4Ggrg2MbGnWO9X8K1t4+fGMDw==", - "dev": true, - "license": "ISC" - }, "node_modules/fsevents": { "version": "2.3.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.3.2.tgz", @@ -3481,28 +3440,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/glob": { - "version": "7.2.3", - "resolved": "https://registry.npmjs.org/glob/-/glob-7.2.3.tgz", - "integrity": "sha512-nFR0zLpU2YCaRxwoCJvL6UvCH2JFyFVIvwTLsIf21AuHlMskA1hhTdk+LlYJtOlYt9v6dvszD2BGRqBL+iQK9Q==", - "deprecated": "Glob versions prior to v9 are no longer supported", - "dev": true, - "license": "ISC", - "dependencies": { - "fs.realpath": "^1.0.0", - "inflight": "^1.0.4", - "inherits": "2", - "minimatch": "^3.1.1", - "once": "^1.3.0", - "path-is-absolute": "^1.0.0" - }, - "engines": { - "node": "*" - }, - "funding": { - "url": "https://github.com/sponsors/isaacs" - } - }, "node_modules/glob-parent": { "version": "6.0.2", "resolved": "https://registry.npmjs.org/glob-parent/-/glob-parent-6.0.2.tgz", @@ -3564,13 +3501,6 @@ "url": "https://github.com/sponsors/ljharb" } }, - "node_modules/graceful-fs": { - "version": "4.2.11", - "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", - "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==", - "dev": true, - "license": "ISC" - }, "node_modules/graphemer": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/graphemer/-/graphemer-1.4.0.tgz", @@ -3722,25 +3652,6 @@ "node": ">=0.8.19" } }, - "node_modules/inflight": { - "version": "1.0.6", - "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", - "integrity": "sha512-k92I/b08q4wvFscXCLvqfsHCrjrF7yiXsQuIVvVE7N82W3+aqpzuUdBbfhWcy/FZR3/4IgflMgKLOsvPDrGCJA==", - "deprecated": "This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.", - "dev": true, - "license": "ISC", - "dependencies": { - "once": "^1.3.0", - "wrappy": "1" - } - }, - "node_modules/inherits": { - "version": "2.0.4", - "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", - "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", - "dev": true, - "license": "ISC" - }, "node_modules/is-binary-path": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/is-binary-path/-/is-binary-path-2.1.0.tgz", @@ -4061,16 +3972,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/min-indent": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/min-indent/-/min-indent-1.0.1.tgz", - "integrity": "sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=4" - } - }, "node_modules/miniflare": { "version": "3.20240821.1", "resolved": "https://registry.npmjs.org/miniflare/-/miniflare-3.20240821.1.tgz", @@ -4126,29 +4027,6 @@ "node": "*" } }, - "node_modules/minimist": { - "version": "1.2.8", - "resolved": "https://registry.npmjs.org/minimist/-/minimist-1.2.8.tgz", - "integrity": "sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA==", - "dev": true, - "license": "MIT", - "funding": { - "url": "https://github.com/sponsors/ljharb" - } - }, - "node_modules/mkdirp": { - "version": "0.5.6", - "resolved": "https://registry.npmjs.org/mkdirp/-/mkdirp-0.5.6.tgz", - "integrity": "sha512-FP+p8RB8OWpF3YZBCrP5gtADmtXApB5AMLn+vdyA+PyxCjrCs00mjyUozssO33cwDeT3wNGdLxJ5M//YqtHAJw==", - "dev": true, - "license": "MIT", - "dependencies": { - "minimist": "^1.2.6" - }, - "bin": { - "mkdirp": "bin/cmd.js" - } - }, "node_modules/mri": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/mri/-/mri-1.2.0.tgz", @@ -4284,16 +4162,6 @@ "license": "MIT", "peer": true }, - "node_modules/once": { - "version": "1.4.0", - "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", - "integrity": "sha512-lNaJgI+2Q5URQBkccEKHTQOPaXdUxnZZElQTZY0MFUAuaEqe1E+Nyvgdz/aIyNi6Z9MzO5dv1H8n58/GELp3+w==", - "dev": true, - "license": "ISC", - "dependencies": { - "wrappy": "1" - } - }, "node_modules/onetime": { "version": "6.0.0", "resolved": "https://registry.npmjs.org/onetime/-/onetime-6.0.0.tgz", @@ -4383,16 +4251,6 @@ "node": ">=8" } }, - "node_modules/path-is-absolute": { - "version": "1.0.1", - "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", - "integrity": "sha512-AVbw3UJ2e9bq64vSaS9Am0fje1Pa8pbGqTTsmXfaIiMpnr5DlDhfJOuLj9Sf95ZPVDAUerDfEk88MPmPe7UCQg==", - "dev": true, - "license": "MIT", - "engines": { - "node": ">=0.10.0" - } - }, "node_modules/path-key": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/path-key/-/path-key-3.1.1.tgz", @@ -4806,20 +4664,6 @@ "node": ">=0.10.0" } }, - "node_modules/rimraf": { - "version": "2.7.1", - "resolved": "https://registry.npmjs.org/rimraf/-/rimraf-2.7.1.tgz", - "integrity": "sha512-uWjbaKIK3T1OSVptzX7Nl6PvQ3qAGtKEtVRjRuazjfL3Bx5eI409VZSqgND+4UNnmzLVdPj9FqFJNPqBZFve4w==", - "deprecated": "Rimraf versions prior to v4 are no longer supported", - "dev": true, - "license": "ISC", - "dependencies": { - "glob": "^7.1.3" - }, - "bin": { - "rimraf": "bin.js" - } - }, "node_modules/rollup": { "version": "4.21.2", "resolved": "https://registry.npmjs.org/rollup/-/rollup-4.21.2.tgz", @@ -4977,19 +4821,6 @@ ], "license": "MIT" }, - "node_modules/sander": { - "version": "0.5.1", - "resolved": "https://registry.npmjs.org/sander/-/sander-0.5.1.tgz", - "integrity": "sha512-3lVqBir7WuKDHGrKRDn/1Ye3kwpXaDOMsiRP1wd6wpZW56gJhsbp5RqQpA6JG/P+pkXizygnr1dKR8vzWaVsfA==", - "dev": true, - "license": "MIT", - "dependencies": { - "es6-promise": "^3.1.2", - "graceful-fs": "^4.1.3", - "mkdirp": "^0.5.1", - "rimraf": "^2.5.2" - } - }, "node_modules/selfsigned": { "version": "2.4.1", "resolved": "https://registry.npmjs.org/selfsigned/-/selfsigned-2.4.1.tgz", @@ -5120,22 +4951,6 @@ "node": ">= 10" } }, - "node_modules/sorcery": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/sorcery/-/sorcery-0.11.1.tgz", - "integrity": "sha512-o7npfeJE6wi6J9l0/5LKshFzZ2rMatRiCDwYeDQaOzqdzRJwALhX7mk/A/ecg6wjMu7wdZbmXfD2S/vpOg0bdQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "@jridgewell/sourcemap-codec": "^1.4.14", - "buffer-crc32": "^1.0.0", - "minimist": "^1.2.0", - "sander": "^0.5.0" - }, - "bin": { - "sorcery": "bin/sorcery" - } - }, "node_modules/source-map": { "version": "0.6.1", "resolved": "https://registry.npmjs.org/source-map/-/source-map-0.6.1.tgz", @@ -5245,19 +5060,6 @@ "url": "https://github.com/sponsors/sindresorhus" } }, - "node_modules/strip-indent": { - "version": "3.0.0", - "resolved": "https://registry.npmjs.org/strip-indent/-/strip-indent-3.0.0.tgz", - "integrity": "sha512-laJTa3Jb+VQpaC6DseHhF7dXVqHTfJPCRDaEbid/drOhgitgYku/letMUqOXFoWV0zIIUbjpdH2t+tYj4bQMRQ==", - "dev": true, - "license": "MIT", - "dependencies": { - "min-indent": "^1.0.0" - }, - "engines": { - "node": ">=8" - } - }, "node_modules/strip-json-comments": { "version": "3.1.1", "resolved": "https://registry.npmjs.org/strip-json-comments/-/strip-json-comments-3.1.1.tgz", @@ -5338,24 +5140,57 @@ } }, "node_modules/svelte-check": { - "version": "3.8.6", - "resolved": "https://registry.npmjs.org/svelte-check/-/svelte-check-3.8.6.tgz", - "integrity": "sha512-ij0u4Lw/sOTREP13BdWZjiXD/BlHE6/e2e34XzmVmsp5IN4kVa3PWP65NM32JAgwjZlwBg/+JtiNV1MM8khu0Q==", + "version": "4.0.1", + "resolved": "https://registry.npmjs.org/svelte-check/-/svelte-check-4.0.1.tgz", + "integrity": "sha512-AuWnCZdREoOzMhoptHPUUPYUxLNdXSkoZnPnlv19SZJJimRzLmjjZLKsOiRB4AnhgX+56/WSEdvkWXI/q2BSsA==", "dev": true, "license": "MIT", "dependencies": { - "@jridgewell/trace-mapping": "^0.3.17", + "@jridgewell/trace-mapping": "^0.3.25", "chokidar": "^3.4.1", + "fdir": "^6.2.0", "picocolors": "^1.0.0", - "sade": "^1.7.4", - "svelte-preprocess": "^5.1.3", - "typescript": "^5.0.3" + "sade": "^1.7.4" }, "bin": { "svelte-check": "bin/svelte-check" }, + "engines": { + "node": ">= 18.0.0" + }, "peerDependencies": { - "svelte": "^3.55.0 || ^4.0.0-next.0 || ^4.0.0 || ^5.0.0-next.0" + "svelte": "^4.0.0 || ^5.0.0-next.0", + "typescript": ">=5.0.0" + } + }, + "node_modules/svelte-check/node_modules/fdir": { + "version": "6.3.0", + "resolved": "https://registry.npmjs.org/fdir/-/fdir-6.3.0.tgz", + "integrity": "sha512-QOnuT+BOtivR77wYvCWHfGt9s4Pz1VIMbD463vegT5MLqNXy8rYFT/lPVEqf/bhYeT6qmqrNHhsX+rWwe3rOCQ==", + "dev": true, + "license": "MIT", + "peerDependencies": { + "picomatch": "^3 || ^4" + }, + "peerDependenciesMeta": { + "picomatch": { + "optional": true + } + } + }, + "node_modules/svelte-check/node_modules/picomatch": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/picomatch/-/picomatch-4.0.2.tgz", + "integrity": "sha512-M7BAV6Rlcy5u+m6oPhAPFgJTzAioX/6B0DxyvDlo9l8+T3nLKbrczg2WLUyzd45L8RqfUMyGPzekbMvX2Ldkwg==", + "dev": true, + "license": "MIT", + "optional": true, + "peer": true, + "engines": { + "node": ">=12" + }, + "funding": { + "url": "https://github.com/sponsors/jonschlinkert" } }, "node_modules/svelte-eslint-parser": { @@ -5434,69 +5269,6 @@ "url": "https://opencollective.com/eslint" } }, - "node_modules/svelte-preprocess": { - "version": "5.1.4", - "resolved": "https://registry.npmjs.org/svelte-preprocess/-/svelte-preprocess-5.1.4.tgz", - "integrity": "sha512-IvnbQ6D6Ao3Gg6ftiM5tdbR6aAETwjhHV+UKGf5bHGYR69RQvF1ho0JKPcbUON4vy4R7zom13jPjgdOWCQ5hDA==", - "dev": true, - "hasInstallScript": true, - "license": "MIT", - "dependencies": { - "@types/pug": "^2.0.6", - "detect-indent": "^6.1.0", - "magic-string": "^0.30.5", - "sorcery": "^0.11.0", - "strip-indent": "^3.0.0" - }, - "engines": { - "node": ">= 16.0.0" - }, - "peerDependencies": { - "@babel/core": "^7.10.2", - "coffeescript": "^2.5.1", - "less": "^3.11.3 || ^4.0.0", - "postcss": "^7 || ^8", - "postcss-load-config": "^2.1.0 || ^3.0.0 || ^4.0.0 || ^5.0.0", - "pug": "^3.0.0", - "sass": "^1.26.8", - "stylus": "^0.55.0", - "sugarss": "^2.0.0 || ^3.0.0 || ^4.0.0", - "svelte": "^3.23.0 || ^4.0.0-next.0 || ^4.0.0 || ^5.0.0-next.0", - "typescript": ">=3.9.5 || ^4.0.0 || ^5.0.0" - }, - "peerDependenciesMeta": { - "@babel/core": { - "optional": true - }, - "coffeescript": { - "optional": true - }, - "less": { - "optional": true - }, - "postcss": { - "optional": true - }, - "postcss-load-config": { - "optional": true - }, - "pug": { - "optional": true - }, - "sass": { - "optional": true - }, - "stylus": { - "optional": true - }, - "sugarss": { - "optional": true - }, - "typescript": { - "optional": true - } - } - }, "node_modules/svelte-stripe": { "version": "1.2.0", "resolved": "https://registry.npmjs.org/svelte-stripe/-/svelte-stripe-1.2.0.tgz", @@ -6499,13 +6271,6 @@ "url": "https://github.com/chalk/wrap-ansi?sponsor=1" } }, - "node_modules/wrappy": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", - "integrity": "sha512-l4Sp/DRseor9wL6EvV2+TuQn63dMkPjZ/sp9XkghTEbV9KlPS1xUsZ3u7/IQO4wxtcFB4bgpQPRcR3QCvezPcQ==", - "dev": true, - "license": "ISC" - }, "node_modules/ws": { "version": "8.18.0", "resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz", diff --git a/package.json b/package.json index 53433c0..a4fea5c 100644 --- a/package.json +++ b/package.json @@ -34,7 +34,7 @@ "prettier-plugin-svelte": "^3.1.2", "stripe": "^16.7.0", "svelte": "^5.0.0-next.244", - "svelte-check": "^3.6.0", + "svelte-check": "^4.0.1", "svelte-stripe": "^1.2.0", "tslib": "^2.4.1", "typescript": "^5.0.0", @@ -49,8 +49,5 @@ } }, "type": "module", - "dependencies": {}, - "volta": { - "node": "20.17.0" - } + "dependencies": {} } diff --git a/src/api/auth.ts b/src/api/auth.ts index 968b9b4..1ef121a 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -1,15 +1,10 @@ import type { Context } from 'hono'; import { getCookie } from 'hono/cookie'; import { createMiddleware } from 'hono/factory'; -import { - Auth, - ServiceAccountCredential, - WorkersKVStoreSingle -} from 'firebase-auth-cloudflare-workers-x509'; -import { GOOGLE_SERVICE_ACCOUNT_KEY } from '$env/static/private'; import { HTTPException } from 'hono/http-exception'; +import { getAuthWithKV } from '$lib/firebase/server'; -type CurrentUser = { +export type CurrentUser = { uid: string; name: string; }; @@ -21,14 +16,9 @@ export interface AuthVariables { export const authMiddleware = createMiddleware(async (c, next) => { const sessionCookie = getCookie(c, 'session'); if (sessionCookie) { - const keys = WorkersKVStoreSingle.getOrInitialize('pubkeys', c.env.KV); - const auth = Auth.getOrInitialize( - 'fukada-delete-me', - keys, - new ServiceAccountCredential(GOOGLE_SERVICE_ACCOUNT_KEY) - ); + const auth = getAuthWithKV(c.env.KV); try { - const idToken = await auth.verifySessionCookie(sessionCookie); + const idToken = await auth.verifySessionCookie(sessionCookie, false); c.set('currentUser', { uid: idToken.uid, name: idToken.name diff --git a/src/api/index.ts b/src/api/index.ts index 0b09ae2..3350424 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -10,13 +10,10 @@ const app = new Hono<{ Bindings: Env; Variables: AuthVariables }>() .use(authMiddleware) .get('/api/posts', async (c) => { const currentUser = ensureUser(c); - const posts: Post[] = []; - for (let i = 0; i < 20; i++) { - posts.push({ - title: '素晴しい記事', - author: currentUser.name - }); - } + const posts = Array.from({ length: 20 }, () => ({ + title: '素晴しい記事', + author: currentUser.name + })); return c.json(posts); }); diff --git a/src/hooks.server.ts b/src/hooks.server.ts index e2920b1..32db849 100644 --- a/src/hooks.server.ts +++ b/src/hooks.server.ts @@ -1,18 +1,17 @@ import { sequence } from '@sveltejs/kit/hooks'; import { type Handle, redirect } from '@sveltejs/kit'; -import { verifySessionCookie } from '$lib/firebase/server'; -import { WorkersKVStoreSingle } from 'firebase-auth-cloudflare-workers-x509'; +import { getAuthWithKV } from '$lib/firebase/server'; /** * セッションクッキーの検証 */ const verifySessionToken: Handle = async ({ event, resolve }) => { - const session = event.cookies.get('session'); - const keys = WorkersKVStoreSingle.getOrInitialize('pubkeys', event.platform?.env?.KV); + const auth = getAuthWithKV(event.platform?.env?.KV); + const session = event.cookies.get('session'); if (session) { try { - const decoded = await verifySessionCookie(keys, session); + const decoded = await auth.verifySessionCookie(session, false); event.locals.currentUser = { uid: decoded.uid, name: decoded.name || '', @@ -29,7 +28,7 @@ const verifySessionToken: Handle = async ({ event, resolve }) => { /** * 認証ガード * - * 未認証ユーザなどを、別のページにリダイレクトするのが良い場合に。 + * 未認証のユーザなどを、別のページにリダイレクトするのが良い場合に。 */ const authGuard: Handle = async ({ event, resolve }) => { const currentUser = event.locals.currentUser; diff --git a/src/lib/firebase/client.ts b/src/lib/firebase/client.ts index 2c14ce0..584bed1 100644 --- a/src/lib/firebase/client.ts +++ b/src/lib/firebase/client.ts @@ -1,13 +1,12 @@ // Firebase Authentication のブラウザ側用のコード -import { GoogleAuthProvider } from 'firebase/auth'; -import { getAuth, signInWithPopup } from 'firebase/auth'; +import { GoogleAuthProvider, getAuth, signInWithPopup } from 'firebase/auth'; import { browser } from '$app/environment'; import { invalidate } from '$app/navigation'; import { initializeApp } from 'firebase/app'; // これらは秘匿情報ではない -const firebaseConfig = { +export const firebaseConfig = { apiKey: 'AIzaSyCqkxdTAOegNHszt2gHJp8Jkss2v9IZ71c', authDomain: 'fukada-delete-me.firebaseapp.com', projectId: 'fukada-delete-me', @@ -19,8 +18,8 @@ const firebaseConfig = { if (browser) { initializeApp(firebaseConfig); - const auth = getAuth(); - auth.onIdTokenChanged((user) => { + // idToken が変わったら、セッショントークンを発行させる + getAuth().onIdTokenChanged((user) => { if (user) { user.getIdToken().then((idToken) => { updateSession(idToken); diff --git a/src/lib/firebase/server.ts b/src/lib/firebase/server.ts index 52c0929..fa382eb 100644 --- a/src/lib/firebase/server.ts +++ b/src/lib/firebase/server.ts @@ -1,37 +1,24 @@ -// Firebase Authentication のサーバ側のコード +// Firebase Authentication のサーバ側用のコード import { Auth, - type KeyStorer, - ServiceAccountCredential + ServiceAccountCredential, + WorkersKVStoreSingle, + type KeyStorer } from 'firebase-auth-cloudflare-workers-x509'; -import { GOOGLE_SERVICE_ACCOUNT_KEY } from '$env/static/private'; - -function getAuth(keys: KeyStorer) { - return Auth.getOrInitialize( - 'fukada-delete-me', - keys, - new ServiceAccountCredential(GOOGLE_SERVICE_ACCOUNT_KEY) - ); -} +import { firebaseConfig } from './client'; -export async function createSessionCookie(keys: KeyStorer, idToken: string, days: number) { - return await getAuth(keys).createSessionCookie(idToken, { - expiresIn: 1000 * 60 * 60 * 24 * days - }); -} +import { GOOGLE_SERVICE_ACCOUNT_KEY } from '$env/static/private'; -export async function verifySessionCookie(keys: KeyStorer, session: string) { - const checkRevoked = false; - return await getAuth(keys).verifySessionCookie(session, checkRevoked); -} +export const projectId = firebaseConfig.projectId; +export const serviceAccountCredential = new ServiceAccountCredential(GOOGLE_SERVICE_ACCOUNT_KEY); -export async function verifyIdToken(keys: KeyStorer, idToken: string) { - const checkRevoked = false; - return await getAuth(keys).verifyIdToken(idToken, checkRevoked); +export function getAuthWithKV(kv: KVNamespace) { + const storer = WorkersKVStoreSingle.getOrInitialize('pubkeys', kv); + return getAuthWithStorer(storer); } -export async function getUser(keys: KeyStorer, uid: string) { - return await getAuth(keys).getUser(uid); +export function getAuthWithStorer(keys: KeyStorer) { + return Auth.getOrInitialize(projectId, keys, serviceAccountCredential); } diff --git a/src/routes/+layout.server.ts b/src/routes/+layout.server.ts index 380b80b..dd5c2e7 100644 --- a/src/routes/+layout.server.ts +++ b/src/routes/+layout.server.ts @@ -1,5 +1,5 @@ export const load = async ({ locals, depends }) => { - // hooks.server.ts で得たユーザ情報を locals 経由で受けとり、data として配下のレイアウトおよびページに渡す + // hooks.server.ts で得たユーザ情報を locals 経由で受けとって、配下のレイアウトおよびページに data として渡す depends('auth:session'); return { currentUser: locals.currentUser diff --git a/src/routes/+page.svelte b/src/routes/+page.svelte index f95bef3..1b922f7 100644 --- a/src/routes/+page.svelte +++ b/src/routes/+page.svelte @@ -1 +1,16 @@ + +