Skip to content

Commit

Permalink
feat(auth, multi-tenant): add multi-tenant (tenantID) support
Browse files Browse the repository at this point in the history
  • Loading branch information
xianlinbox authored and mikehardy committed Mar 12, 2021
1 parent d2b0074 commit 935dbc3
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/auth/__tests__/auth.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down
7 changes: 7 additions & 0 deletions packages/auth/ios/RNFBAuth/RNFBAuthModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -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
) {
Expand Down
22 changes: 22 additions & 0 deletions packages/auth/lib/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<void>;
/**
* Sets the language code.
*
Expand Down
13 changes: 13 additions & 0 deletions packages/auth/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]'];
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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)
Expand Down

0 comments on commit 935dbc3

Please sign in to comment.