Skip to content

Commit 6767bb8

Browse files
committed
Add methods for validating if TenantConfig is initialized
1 parent e2d7605 commit 6767bb8

File tree

7 files changed

+999
-1045
lines changed

7 files changed

+999
-1045
lines changed

packages/auth/demo/src/index.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -2046,7 +2046,13 @@ function onApplyAuthSettingsChange() {
20462046
function initApp() {
20472047
log('Initializing app...');
20482048
app = initializeApp(config);
2049-
auth = getAuth(app);
2049+
const tenantConfig: {'location': "us", 'tenantId': 'tenant-1'};
2050+
// auth = getAuth(app);
2051+
auth = initializeAuth(app, {
2052+
tenantConfig: tenantConfig
2053+
});
2054+
console.log('demo app');
2055+
console.log(auth);
20502056
if (USE_AUTH_EMULATOR) {
20512057
connectAuthEmulator(auth, AUTH_EMULATOR_URL);
20522058
}

packages/auth/demo/src/worker/service-worker.ts

+29-29
Original file line numberDiff line numberDiff line change
@@ -74,19 +74,19 @@ function getOriginFromUrl(url: string): string {
7474
return protocol + '//' + host;
7575
}
7676

77-
self.addEventListener('install', (event: ExtendableEvent) => {
78-
// Perform install steps.
79-
event.waitUntil(async (): Promise<void> => {
80-
const cache = await caches.open(CACHE_NAME);
81-
// Add all URLs of resources we want to cache.
82-
try {
83-
await cache.addAll(urlsToCache);
84-
} catch {
85-
// Suppress error as some of the files may not be available for the
86-
// current page.
87-
}
88-
});
89-
});
77+
// self.addEventListener('install', (event: ExtendableEvent) => {
78+
// // Perform install steps.
79+
// event.waitUntil(async (): Promise<void> => {
80+
// const cache = await caches.open(CACHE_NAME);
81+
// // Add all URLs of resources we want to cache.
82+
// try {
83+
// await cache.addAll(urlsToCache);
84+
// } catch {
85+
// // Suppress error as some of the files may not be available for the
86+
// // current page.
87+
// }
88+
// });
89+
// });
9090

9191
// As this is a test app, let's only return cached data when offline.
9292
self.addEventListener('fetch', (event: FetchEvent) => {
@@ -155,19 +155,19 @@ self.addEventListener('fetch', (event: FetchEvent) => {
155155
);
156156
});
157157

158-
self.addEventListener('activate', (event: ExtendableEvent) => {
159-
// Update this list with all caches that need to remain cached.
160-
const cacheWhitelist = ['cache-v1'];
161-
event.waitUntil(async (): Promise<void> => {
162-
const cacheNames = await caches.keys();
163-
await Promise.all(
164-
cacheNames.map(cacheName => {
165-
// Check if cache is not whitelisted above.
166-
if (cacheWhitelist.indexOf(cacheName) === -1) {
167-
// If not whitelisted, delete it.
168-
return caches.delete(cacheName);
169-
}
170-
})
171-
);
172-
});
173-
});
158+
// self.addEventListener('activate', (event: ExtendableEvent) => {
159+
// // Update this list with all caches that need to remain cached.
160+
// const cacheWhitelist = ['cache-v1'];
161+
// event.waitUntil(async (): Promise<void> => {
162+
// const cacheNames = await caches.keys();
163+
// await Promise.all(
164+
// cacheNames.map(cacheName => {
165+
// // Check if cache is not whitelisted above.
166+
// if (cacheWhitelist.indexOf(cacheName) === -1) {
167+
// // If not whitelisted, delete it.
168+
// return caches.delete(cacheName);
169+
// }
170+
// })
171+
// );
172+
// });
173+
// });

packages/auth/internal/index.ts

+13-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18-
import { _castAuth } from '../src/core/auth/auth_impl';
18+
import { _castAuth, AuthImpl } from '../src/core/auth/auth_impl';
1919
import { Auth } from '../src/model/public_types';
2020

2121
/**
@@ -56,3 +56,15 @@ export { SAMLAuthCredential } from '../src/core/credentials/saml';
5656
export function addFrameworkForLogging(auth: Auth, framework: string): void {
5757
_castAuth(auth)._logFramework(framework);
5858
}
59+
60+
/**
61+
*
62+
* @param auth - an object of type AuthImpl.
63+
*
64+
* @returns true if the provided AuthImpl object contains location in tenantConfig.
65+
*
66+
* @internal
67+
*/
68+
export function _isFirebaseRegionalAuthInitialized(auth: AuthImpl): boolean {
69+
return !!(auth.tenantConfig.location && auth.tenantConfig.tenantId);
70+
}

packages/auth/src/core/auth/auth_impl.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ import {
3636
ErrorFn,
3737
NextFn,
3838
Unsubscribe,
39-
PasswordValidationStatus
39+
PasswordValidationStatus,
40+
TenantConfig
4041
} from '../../model/public_types';
4142
import {
4243
createSubscribe,
@@ -126,6 +127,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
126127
| undefined = undefined;
127128
_persistenceManagerAvailable: Promise<void>;
128129
readonly name: string;
130+
readonly tenantConfig: TenantConfig;
129131

130132
// Tracks the last notified UID for state change listeners to prevent
131133
// repeated calls to the callbacks. Undefined means it's never been
@@ -140,7 +142,8 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
140142
public readonly app: FirebaseApp,
141143
private readonly heartbeatServiceProvider: Provider<'heartbeat'>,
142144
private readonly appCheckServiceProvider: Provider<AppCheckInternalComponentName>,
143-
public readonly config: ConfigInternal
145+
public readonly config: ConfigInternal,
146+
tenantConfig?: TenantConfig
144147
) {
145148
this.name = app.name;
146149
this.clientVersion = config.sdkClientVersion;
@@ -149,6 +152,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
149152
this._persistenceManagerAvailable = new Promise<void>(
150153
resolve => (this._resolvePersistenceManagerAvailable = resolve)
151154
);
155+
this.tenantConfig = tenantConfig ?? {};
152156
}
153157

154158
_initializeWithPersistence(

packages/auth/src/core/strategies/email_and_password.ts

+6-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ import { EmailAuthProvider } from '../providers/email';
3131
import { UserCredentialImpl } from '../user/user_credential_impl';
3232
import {
3333
_assert,
34-
_serverAppCurrentUserOperationNotSupportedError
34+
_serverAppCurrentUserOperationNotSupportedError,
35+
_operationNotSupportedForInitializedAuthInstance
3536
} from '../util/assert';
3637
import { _setActionCodeSettingsOnRequest } from './action_code_settings';
3738
import { signInWithCredential } from './credential';
@@ -47,6 +48,7 @@ import {
4748
RecaptchaAuthProvider
4849
} from '../../api';
4950
import { _isFirebaseServerApp } from '@firebase/app';
51+
import { _isFirebaseRegionalAuthInitialized } from '../../../internal';
5052

5153
/**
5254
* Updates the password policy cached in the {@link Auth} instance if a policy is already
@@ -352,6 +354,9 @@ export function signInWithEmailAndPassword(
352354
_serverAppCurrentUserOperationNotSupportedError(auth)
353355
);
354356
}
357+
if (_isFirebaseRegionalAuthInitialized(auth)) {
358+
return Promise.reject(_operationNotSupportedForInitializedAuthInstance(auth));
359+
}
355360
return signInWithCredential(
356361
getModularInstance(auth),
357362
EmailAuthProvider.credential(email, password)

packages/auth/src/core/util/assert.ts

+8
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,14 @@ export function _serverAppCurrentUserOperationNotSupportedError(
112112
);
113113
}
114114

115+
export function _operationNotSupportedForInitializedAuthInstance(auth: Auth) : FirebaseError {
116+
return _errorWithCustomMessage(
117+
auth,
118+
AuthErrorCode.OPERATION_NOT_ALLOWED,
119+
'Operations not allowed for the auth object initialized.'
120+
);
121+
}
122+
115123
export function _assertInstanceOf(
116124
auth: Auth,
117125
object: object,

0 commit comments

Comments
 (0)