From 935dbc30515425949b4c9053da0db7f76b7a318f Mon Sep 17 00:00:00 2001 From: Xianning Liu Date: Thu, 11 Mar 2021 23:46:13 -0600 Subject: [PATCH] feat(auth, multi-tenant): add multi-tenant (tenantID) support --- packages/auth/__tests__/auth.test.ts | 19 ++++++++++++++++ .../auth/ReactNativeFirebaseAuthModule.java | 13 +++++++++++ packages/auth/ios/RNFBAuth/RNFBAuthModule.m | 7 ++++++ packages/auth/lib/index.d.ts | 22 +++++++++++++++++++ packages/auth/lib/index.js | 13 +++++++++++ 5 files changed, 74 insertions(+) diff --git a/packages/auth/__tests__/auth.test.ts b/packages/auth/__tests__/auth.test.ts index d733145aa0..f06a18c2a7 100644 --- a/packages/auth/__tests__/auth.test.ts +++ b/packages/auth/__tests__/auth.test.ts @@ -43,4 +43,23 @@ describe('Auth', function () { expect(bar).toEqual(['10.0.2.2', 9099]); }); }); + + describe('tenantId', function () { + it('should be able to set tenantId ', function () { + const auth = firebase.app().auth(); + auth.setTenantId('test-id').then(() => { + expect(auth.tenantId).toBe('test-id'); + }); + }); + + it('should throw error when tenantId is a non string object ', async function () { + try { + await firebase.app().auth().setTenantId(Object()); + return Promise.reject('It should throw an error'); + } catch (e) { + expect(e.message).toBe("firebase.auth().setTenantId(*) expected 'tenantId' to be a string"); + return Promise.resolve('Error catched'); + } + }); + }); }); diff --git a/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java b/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java index c6491237a7..cfd7626d42 100644 --- a/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java +++ b/packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java @@ -1627,6 +1627,19 @@ public void setLanguageCode(String appName, String code) { } } + /** + * setTenantId + * + * @param appName + * @param tenantId + */ + @ReactMethod + public void setTenantId(String appName, String tenantId) { + FirebaseApp firebaseApp = FirebaseApp.getInstance(appName); + FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp); + firebaseAuth.setTenantId(tenantId); + } + /** * useDeviceLanguage * diff --git a/packages/auth/ios/RNFBAuth/RNFBAuthModule.m b/packages/auth/ios/RNFBAuth/RNFBAuthModule.m index 232ba9c125..8bc45e73ff 100644 --- a/packages/auth/ios/RNFBAuth/RNFBAuthModule.m +++ b/packages/auth/ios/RNFBAuth/RNFBAuthModule.m @@ -900,6 +900,13 @@ - (void)invalidate { } +RCT_EXPORT_METHOD(setTenantId: + (FIRApp *) firebaseApp + :(NSString *) tenantID +) { + FIRAuth authWithApp:firebaseApp].tenantID = tenantID; +} + RCT_EXPORT_METHOD(useDeviceLanguage: (FIRApp *) firebaseApp ) { diff --git a/packages/auth/lib/index.d.ts b/packages/auth/lib/index.d.ts index a276aeaf86..4c750551a0 100644 --- a/packages/auth/lib/index.d.ts +++ b/packages/auth/lib/index.d.ts @@ -1201,6 +1201,16 @@ export namespace FirebaseAuthTypes { * TODO @salakar missing updateCurrentUser */ export class Module extends FirebaseModule { + /** + * Returns the current tenant Id or null if it has never been set + * + * #### Example + * + * ```js + * const tenantId = firebase.auth().tenantId; + * ``` + */ + tenantId: string | null; /** * Returns the current language code. * @@ -1228,6 +1238,18 @@ export namespace FirebaseAuthTypes { * > It is recommended to use {@link auth#onAuthStateChanged} to track whether the user is currently signed in. */ currentUser: User | null; + /** + * Sets the tenant id. + * + * #### Example + * + * ```js + * await firebase.auth().setTenantId('tenant-123'); + * ``` + * + * @param tenantId the tenantID current app bind to. + */ + setTenantId(tenantId: string): Promise; /** * Sets the language code. * diff --git a/packages/auth/lib/index.js b/packages/auth/lib/index.js index 57d4c77247..36f1f069e7 100644 --- a/packages/auth/lib/index.js +++ b/packages/auth/lib/index.js @@ -69,6 +69,7 @@ class FirebaseAuthModule extends FirebaseModule { this._settings = null; this._authResult = false; this._languageCode = this.native.APP_LANGUAGE[this.app._name]; + this._tenantId = null; if (!this.languageCode) { this._languageCode = this.native.APP_LANGUAGE['[DEFAULT]']; @@ -101,6 +102,10 @@ class FirebaseAuthModule extends FirebaseModule { return this._languageCode; } + get tenantId() { + return this._tenantId; + } + get settings() { if (!this._settings) { this._settings = new Settings(this); @@ -150,6 +155,14 @@ class FirebaseAuthModule extends FirebaseModule { } } + async setTenantId(tenantId) { + if (!isString(tenantId)) { + throw new Error("firebase.auth().setTenantId(*) expected 'tenantId' to be a string"); + } + this._tenantId = tenantId; + await this.native.setTenantId(tenantId); + } + _parseListener(listenerOrObserver) { return typeof listenerOrObserver === 'object' ? listenerOrObserver.next.bind(listenerOrObserver)