Skip to content

Commit c8d9cb5

Browse files
committed
Use getToken instead of getFirebaseToken
1 parent a5a79f1 commit c8d9cb5

File tree

3 files changed

+35
-23
lines changed

3 files changed

+35
-23
lines changed

common/api-review/auth.api.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ export interface Auth {
8888
readonly config: Config;
8989
readonly currentUser: User | null;
9090
readonly emulatorConfig: EmulatorConfig | null;
91+
readonly firebaseToken: FirebaseToken | null;
9192
languageCode: string | null;
9293
readonly name: string;
9394
onAuthStateChanged(nextOrObserver: NextOrObserver<User | null>, error?: ErrorFn, completed?: CompleteFn): Unsubscribe;
@@ -364,7 +365,7 @@ export interface EmulatorConfig {
364365

365366
export { ErrorFn }
366367

367-
// @public (undocumented)
368+
// @public
368369
export function exchangeToken(auth: Auth, idpConfigId: string, customToken: string): Promise<string>;
369370

370371
// Warning: (ae-forgotten-export) The symbol "BaseOAuthProvider" needs to be exported by the entry point index.d.ts
@@ -388,6 +389,14 @@ export const FactorId: {
388389
// @public
389390
export function fetchSignInMethodsForEmail(auth: Auth, email: string): Promise<string[]>;
390391

392+
// @public (undocumented)
393+
export interface FirebaseToken {
394+
// (undocumented)
395+
readonly expirationTime: number;
396+
// (undocumented)
397+
readonly token: string;
398+
}
399+
391400
// @public
392401
export function getAdditionalUserInfo(userCredential: UserCredential): AdditionalUserInfo | null;
393402

packages/auth-interop-types/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ export interface FirebaseAuthTokenData {
2121

2222
export interface FirebaseAuthInternal {
2323
getToken(refreshToken?: boolean): Promise<FirebaseAuthTokenData | null>;
24-
getFirebaseToken(): Promise<FirebaseAuthTokenData | null>;
2524
getUid(): string | null;
2625
addAuthTokenListener(fn: (token: string | null) => void): void;
2726
removeAuthTokenListener(fn: (token: string | null) => void): void;

packages/auth/src/core/auth/firebase_internal.ts

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import { AuthInternal } from '../../model/auth';
2222
import { UserInternal } from '../../model/user';
2323
import { _assert } from '../util/assert';
2424
import { AuthErrorCode } from '../errors';
25+
import { _logWarn } from '../util/log';
2526

2627
interface TokenListener {
2728
(tok: string | null): unknown;
@@ -44,6 +45,12 @@ export class AuthInterop implements FirebaseAuthInternal {
4445
): Promise<{ accessToken: string } | null> {
4546
this.assertAuthConfigured();
4647
await this.auth._initializationPromise;
48+
if (this.auth.tenantConfig) {
49+
if (forceRefresh) {
50+
_logWarn("Refresh token is not a valid operation for Regional Auth instance initialized.");
51+
}
52+
return this.getTokenForRegionalAuth();
53+
}
4754
if (!this.auth.currentUser) {
4855
return null;
4956
}
@@ -52,27 +59,6 @@ export class AuthInterop implements FirebaseAuthInternal {
5259
return { accessToken };
5360
}
5461

55-
async getFirebaseToken(): Promise<{ accessToken: string } | null> {
56-
this.assertAuthConfigured();
57-
await this.auth._initializationPromise;
58-
this.assertRegionalAuthConfigured();
59-
if (!this.auth.firebaseToken) {
60-
return null;
61-
}
62-
63-
if (
64-
!this.auth.firebaseToken.expirationTime ||
65-
Date.now() >
66-
this.auth.firebaseToken.expirationTime - this.TOKEN_EXPIRATION_BUFFER
67-
) {
68-
await this.auth._updateFirebaseToken(null);
69-
return null;
70-
}
71-
72-
const accessToken = await this.auth.firebaseToken.token;
73-
return { accessToken };
74-
}
75-
7662
addAuthTokenListener(listener: TokenListener): void {
7763
this.assertAuthConfigured();
7864
if (this.internalListeners.has(listener)) {
@@ -118,4 +104,22 @@ export class AuthInterop implements FirebaseAuthInternal {
118104
this.auth._stopProactiveRefresh();
119105
}
120106
}
107+
108+
private async getTokenForRegionalAuth(): Promise<{ accessToken: string } | null> {
109+
if (!this.auth.firebaseToken) {
110+
return null;
111+
}
112+
113+
if (
114+
!this.auth.firebaseToken.expirationTime ||
115+
Date.now() >
116+
this.auth.firebaseToken.expirationTime - this.TOKEN_EXPIRATION_BUFFER
117+
) {
118+
await this.auth._updateFirebaseToken(null);
119+
return null;
120+
}
121+
122+
const accessToken = await this.auth.firebaseToken.token;
123+
return { accessToken };
124+
}
121125
}

0 commit comments

Comments
 (0)