diff --git a/package.json b/package.json index a0788aa..b8518e0 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@mierune/sveltekit-firebase-auth-ssr", - "version": "0.0.16", + "version": "0.0.17", "scripts": { "dev": "vite dev", "build": "vite build && npm run package", diff --git a/src/hooks.client.ts b/src/hooks.client.ts index fd66dba..f2f8853 100644 --- a/src/hooks.client.ts +++ b/src/hooks.client.ts @@ -23,4 +23,4 @@ initializeApp({ }); // 認証ライブラリのセットアップ -setupAuthClient(PUBLIC_FIREBASE_AUTH_EMULATOR_HOST); +setupAuthClient({ emulatorHost: PUBLIC_FIREBASE_AUTH_EMULATOR_HOST }); diff --git a/src/lib/firebase-auth/server.ts b/src/lib/firebase-auth/server.ts index a34908f..ed48dc6 100644 --- a/src/lib/firebase-auth/server.ts +++ b/src/lib/firebase-auth/server.ts @@ -2,9 +2,9 @@ import { Auth, - ServiceAccountCredential, type FirebaseIdToken, - type KeyStorer + type KeyStorer, + type Credential } from 'firebase-auth-cloudflare-workers-x509'; import { type Handle, redirect, error, type Cookies } from '@sveltejs/kit'; import { env } from '$env/dynamic/public'; @@ -18,7 +18,7 @@ export { export type AuthHandleOptions = { projectId: string; - serviceAccountCredential?: ServiceAccountCredential; + serviceAccountCredential?: Credential; keyStore: (platform: Readonly | undefined) => KeyStorer; guardPathPattern?: RegExp; }; @@ -28,6 +28,15 @@ const emulatorEnv = { FIREBASE_AUTH_EMULATOR_HOST: env.PUBLIC_FIREBASE_AUTH_EMULATOR_HOST }; +export class NopCredential implements Credential { + async getAccessToken() { + return { + access_token: 'owner', + expires_in: 9 * 3600 + }; + } +} + /** * 認証ミドルウェア */ @@ -37,8 +46,12 @@ export function createAuthHandle({ keyStore: keyStoreMaker }: AuthHandleOptions): Handle { return async ({ event, resolve }) => { - if (!serviceAccountCredential && !emulatorEnv.FIREBASE_AUTH_EMULATOR_HOST) { - console.error('FIREBASE_SERVICE_ACCOUNT_KEY is not set. Authentication will not work.'); + if (!serviceAccountCredential) { + if (emulatorEnv.FIREBASE_AUTH_EMULATOR_HOST) { + serviceAccountCredential ||= new NopCredential(); + } else { + console.error('service account credential is not set. Authentication will not work.'); + } } const auth = getAuth(projectId, keyStoreMaker(event.platform), serviceAccountCredential); @@ -76,11 +89,7 @@ export function createAuthHandle({ }; } -export function getAuth( - projectId: string, - keyStore: KeyStorer, - credential?: ServiceAccountCredential -) { +export function getAuth(projectId: string, keyStore: KeyStorer, credential?: Credential) { return Auth.getOrInitialize(projectId, keyStore, credential); }