Skip to content

feat(auth, multi-tenant): add multi-tenant (tenantID) support #5004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 16 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
24 changes: 24 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,20 @@ 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