Skip to content

Commit efb6ac3

Browse files
committed
Changes to initialize regional getAuth instance
1 parent 1df3d26 commit efb6ac3

File tree

4 files changed

+40
-10
lines changed

4 files changed

+40
-10
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import {
3030
NextOrObserver,
3131
Persistence,
3232
PopupRedirectResolver,
33+
RegionData,
3334
User,
3435
UserCredential,
3536
CompleteFn,
@@ -91,7 +92,8 @@ interface AsyncAction {
9192
export const enum DefaultConfig {
9293
TOKEN_API_HOST = 'securetoken.googleapis.com',
9394
API_HOST = 'identitytoolkit.googleapis.com',
94-
API_SCHEME = 'https'
95+
API_SCHEME = 'https',
96+
REGIONAL_API_HOST = 'identityplatform.googleapis.com'
9597
}
9698

9799
export class AuthImpl implements AuthInternal, _FirebaseService {
@@ -125,6 +127,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
125127
| undefined = undefined;
126128
_persistenceManagerAvailable: Promise<void>;
127129
readonly name: string;
130+
readonly regionData: RegionData;
128131

129132
// Tracks the last notified UID for state change listeners to prevent
130133
// repeated calls to the callbacks. Undefined means it's never been
@@ -139,7 +142,8 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
139142
public readonly app: FirebaseApp,
140143
private readonly heartbeatServiceProvider: Provider<'heartbeat'>,
141144
private readonly appCheckServiceProvider: Provider<AppCheckInternalComponentName>,
142-
public readonly config: ConfigInternal
145+
public readonly config: ConfigInternal,
146+
regionData?: RegionData
143147
) {
144148
this.name = app.name;
145149
this.clientVersion = config.sdkClientVersion;
@@ -148,6 +152,7 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
148152
this._persistenceManagerAvailable = new Promise<void>(
149153
resolve => (this._resolvePersistenceManagerAvailable = resolve)
150154
);
155+
this.regionData = regionData ?? {};
151156
}
152157

153158
_initializeWithPersistence(

packages/auth/src/core/auth/register.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ function getVersionForPlatform(
5858

5959
/** @internal */
6060
export function registerAuth(clientPlatform: ClientPlatform): void {
61+
console.log("inside register auth");
6162
_registerComponent(
6263
new Component(
6364
_ComponentName.AUTH,
@@ -68,7 +69,8 @@ export function registerAuth(clientPlatform: ClientPlatform): void {
6869
const appCheckServiceProvider =
6970
container.getProvider<'app-check-internal'>('app-check-internal');
7071
const { apiKey, authDomain } = app.options;
71-
72+
const regionData = deps?.regionData ?? { location: '', tenantId: '' };
73+
console.log("inside register auth new component");
7274
_assert(
7375
apiKey && !apiKey.includes(':'),
7476
AuthErrorCode.INVALID_API_KEY,
@@ -79,7 +81,7 @@ export function registerAuth(clientPlatform: ClientPlatform): void {
7981
apiKey,
8082
authDomain,
8183
clientPlatform,
82-
apiHost: DefaultConfig.API_HOST,
84+
apiHost: regionData.location ? DefaultConfig.REGIONAL_API_HOST : DefaultConfig.API_HOST,
8385
tokenApiHost: DefaultConfig.TOKEN_API_HOST,
8486
apiScheme: DefaultConfig.API_SCHEME,
8587
sdkClientVersion: _getClientVersion(clientPlatform)
@@ -89,7 +91,8 @@ export function registerAuth(clientPlatform: ClientPlatform): void {
8991
app,
9092
heartbeatServiceProvider,
9193
appCheckServiceProvider,
92-
config
94+
config,
95+
regionData
9396
);
9497
_initializeAuthInstance(authInstance, deps);
9598

packages/auth/src/model/public_types.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,12 @@ export interface Dependencies {
12601260
* Which {@link AuthErrorMap} to use.
12611261
*/
12621262
errorMap?: AuthErrorMap;
1263+
regionData?: RegionData
1264+
}
1265+
1266+
export interface RegionData {
1267+
location?: string;
1268+
tenantId?: string;
12631269
}
12641270

12651271
/**

packages/auth/src/platform_browser/index.ts

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,12 @@ import { browserLocalPersistence } from './persistence/local_storage';
2929
import { browserSessionPersistence } from './persistence/session_storage';
3030
import { indexedDBLocalPersistence } from './persistence/indexed_db';
3131
import { browserPopupRedirectResolver } from './popup_redirect';
32-
import { Auth, User } from '../model/public_types';
33-
import { getDefaultEmulatorHost, getExperimentalSetting } from '@firebase/util';
32+
import { Auth, Dependencies, RegionData, User } from '../model/public_types';
33+
import { deepEqual, getDefaultEmulatorHost, getExperimentalSetting } from '@firebase/util';
3434
import { _setExternalJSProvider } from './load_js';
35-
import { _createError } from '../core/util/assert';
35+
import { _createError, _fail } from '../core/util/assert';
3636
import { AuthErrorCode } from '../core/errors';
37+
import { AuthImpl } from '../core/auth/auth_impl';
3738

3839
const DEFAULT_ID_TOKEN_MAX_AGE = 5 * 60;
3940
const authIdTokenMaxAge =
@@ -73,10 +74,22 @@ const mintCookieFactory = (url: string) => async (user: User | null) => {
7374
*
7475
* @public
7576
*/
76-
export function getAuth(app: FirebaseApp = getApp()): Auth {
77+
export function getAuth(app: FirebaseApp = getApp(), regionData?: RegionData): Auth {
7778
const provider = _getProvider(app, 'auth');
7879

7980
if (provider.isInitialized()) {
81+
const auth = provider.getImmediate() as AuthImpl;
82+
// Extract previously used regionData from initialization
83+
const initialOptions = provider.getOptions() as Dependencies;
84+
85+
const previousRegionData = initialOptions?.regionData ?? {};
86+
const currentRegionData = regionData ?? {};
87+
if (deepEqual(previousRegionData, currentRegionData)) {
88+
return auth;
89+
} else {
90+
_fail(auth, AuthErrorCode.ALREADY_INITIALIZED);
91+
}
92+
8093
return provider.getImmediate();
8194
}
8295

@@ -86,7 +99,8 @@ export function getAuth(app: FirebaseApp = getApp()): Auth {
8699
indexedDBLocalPersistence,
87100
browserLocalPersistence,
88101
browserSessionPersistence
89-
]
102+
],
103+
regionData
90104
});
91105

92106
const authTokenSyncPath = getExperimentalSetting('authTokenSyncURL');
@@ -111,6 +125,8 @@ export function getAuth(app: FirebaseApp = getApp()): Auth {
111125
if (authEmulatorHost) {
112126
connectAuthEmulator(auth, `http://${authEmulatorHost}`);
113127
}
128+
console.log("here");
129+
console.log(auth.config.apiHost);
114130

115131
return auth;
116132
}

0 commit comments

Comments
 (0)