From f2ce1ae6627af837395115774459b8fb7ba523d8 Mon Sep 17 00:00:00 2001 From: Himself65 Date: Fri, 24 Feb 2023 11:59:53 -0600 Subject: [PATCH] feat: connect firebase emulator (#1196) --- .../data-center/src/provider/affine/apis/google.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/packages/data-center/src/provider/affine/apis/google.ts b/packages/data-center/src/provider/affine/apis/google.ts index c99d9b48e6..3b4e4e6083 100644 --- a/packages/data-center/src/provider/affine/apis/google.ts +++ b/packages/data-center/src/provider/affine/apis/google.ts @@ -1,6 +1,6 @@ import { DebugLogger } from '@affine/debug'; import { initializeApp } from 'firebase/app'; -import type { User } from 'firebase/auth'; +import { connectAuthEmulator, User } from 'firebase/auth'; import { type Auth as FirebaseAuth, getAuth as getFirebaseAuth, @@ -203,6 +203,9 @@ export function createGoogleAuth(bareAuth: KyInstance): GoogleAuth { return new GoogleAuth(bareAuth); } +// Connect emulators based on env vars +const envConnectEmulators = process.env.REACT_APP_FIREBASE_EMULATORS === 'true'; + export const getAuthorizer = (googleAuth: GoogleAuth) => { let _firebaseAuth: FirebaseAuth | null = null; const logger = new DebugLogger('authorizer'); @@ -223,6 +226,12 @@ export const getAuthorizer = (googleAuth: GoogleAuth) => { }); _firebaseAuth = getFirebaseAuth(app); } + if (envConnectEmulators && !(window as any).firebaseAuthEmulatorStarted) { + connectAuthEmulator(_firebaseAuth, 'http://localhost:9099', { + disableWarnings: true, + }); + (window as any).firebaseAuthEmulatorStarted = true; + } return _firebaseAuth; } catch (error) { logger.error('Failed to initialize firebase', error);