Skip to content

Commit 935dbc3

Browse files
xianlinboxmikehardy
authored andcommitted
feat(auth, multi-tenant): add multi-tenant (tenantID) support
1 parent d2b0074 commit 935dbc3

File tree

5 files changed

+74
-0
lines changed

5 files changed

+74
-0
lines changed

packages/auth/__tests__/auth.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,23 @@ describe('Auth', function () {
4343
expect(bar).toEqual(['10.0.2.2', 9099]);
4444
});
4545
});
46+
47+
describe('tenantId', function () {
48+
it('should be able to set tenantId ', function () {
49+
const auth = firebase.app().auth();
50+
auth.setTenantId('test-id').then(() => {
51+
expect(auth.tenantId).toBe('test-id');
52+
});
53+
});
54+
55+
it('should throw error when tenantId is a non string object ', async function () {
56+
try {
57+
await firebase.app().auth().setTenantId(Object());
58+
return Promise.reject('It should throw an error');
59+
} catch (e) {
60+
expect(e.message).toBe("firebase.auth().setTenantId(*) expected 'tenantId' to be a string");
61+
return Promise.resolve('Error catched');
62+
}
63+
});
64+
});
4665
});

packages/auth/android/src/main/java/io/invertase/firebase/auth/ReactNativeFirebaseAuthModule.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,19 @@ public void setLanguageCode(String appName, String code) {
16271627
}
16281628
}
16291629

1630+
/**
1631+
* setTenantId
1632+
*
1633+
* @param appName
1634+
* @param tenantId
1635+
*/
1636+
@ReactMethod
1637+
public void setTenantId(String appName, String tenantId) {
1638+
FirebaseApp firebaseApp = FirebaseApp.getInstance(appName);
1639+
FirebaseAuth firebaseAuth = FirebaseAuth.getInstance(firebaseApp);
1640+
firebaseAuth.setTenantId(tenantId);
1641+
}
1642+
16301643
/**
16311644
* useDeviceLanguage
16321645
*

packages/auth/ios/RNFBAuth/RNFBAuthModule.m

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,13 @@ - (void)invalidate {
900900

901901
}
902902

903+
RCT_EXPORT_METHOD(setTenantId:
904+
(FIRApp *) firebaseApp
905+
:(NSString *) tenantID
906+
) {
907+
FIRAuth authWithApp:firebaseApp].tenantID = tenantID;
908+
}
909+
903910
RCT_EXPORT_METHOD(useDeviceLanguage:
904911
(FIRApp *) firebaseApp
905912
) {

packages/auth/lib/index.d.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,6 +1201,16 @@ export namespace FirebaseAuthTypes {
12011201
* TODO @salakar missing updateCurrentUser
12021202
*/
12031203
export class Module extends FirebaseModule {
1204+
/**
1205+
* Returns the current tenant Id or null if it has never been set
1206+
*
1207+
* #### Example
1208+
*
1209+
* ```js
1210+
* const tenantId = firebase.auth().tenantId;
1211+
* ```
1212+
*/
1213+
tenantId: string | null;
12041214
/**
12051215
* Returns the current language code.
12061216
*
@@ -1228,6 +1238,18 @@ export namespace FirebaseAuthTypes {
12281238
* > It is recommended to use {@link auth#onAuthStateChanged} to track whether the user is currently signed in.
12291239
*/
12301240
currentUser: User | null;
1241+
/**
1242+
* Sets the tenant id.
1243+
*
1244+
* #### Example
1245+
*
1246+
* ```js
1247+
* await firebase.auth().setTenantId('tenant-123');
1248+
* ```
1249+
*
1250+
* @param tenantId the tenantID current app bind to.
1251+
*/
1252+
setTenantId(tenantId: string): Promise<void>;
12311253
/**
12321254
* Sets the language code.
12331255
*

packages/auth/lib/index.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ class FirebaseAuthModule extends FirebaseModule {
6969
this._settings = null;
7070
this._authResult = false;
7171
this._languageCode = this.native.APP_LANGUAGE[this.app._name];
72+
this._tenantId = null;
7273

7374
if (!this.languageCode) {
7475
this._languageCode = this.native.APP_LANGUAGE['[DEFAULT]'];
@@ -101,6 +102,10 @@ class FirebaseAuthModule extends FirebaseModule {
101102
return this._languageCode;
102103
}
103104

105+
get tenantId() {
106+
return this._tenantId;
107+
}
108+
104109
get settings() {
105110
if (!this._settings) {
106111
this._settings = new Settings(this);
@@ -150,6 +155,14 @@ class FirebaseAuthModule extends FirebaseModule {
150155
}
151156
}
152157

158+
async setTenantId(tenantId) {
159+
if (!isString(tenantId)) {
160+
throw new Error("firebase.auth().setTenantId(*) expected 'tenantId' to be a string");
161+
}
162+
this._tenantId = tenantId;
163+
await this.native.setTenantId(tenantId);
164+
}
165+
153166
_parseListener(listenerOrObserver) {
154167
return typeof listenerOrObserver === 'object'
155168
? listenerOrObserver.next.bind(listenerOrObserver)

0 commit comments

Comments
 (0)