Skip to content

Commit 9a9f647

Browse files
authored
[Auth] Add conversion function to all modular SDK entry points (#4677)
* [Auth] Add conversion function to all modular SDK entry points * Formatting * Add missing method * Formatting
1 parent 976c5b1 commit 9a9f647

File tree

17 files changed

+114
-73
lines changed

17 files changed

+114
-73
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import {
3434
import {
3535
createSubscribe,
3636
ErrorFactory,
37+
getModularInstance,
3738
Observer,
3839
Subscribe
3940
} from '@firebase/util';
@@ -298,7 +299,9 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
298299
async updateCurrentUser(userExtern: User | null): Promise<void> {
299300
// The public updateCurrentUser method needs to make a copy of the user,
300301
// and also check that the project matches
301-
const user = userExtern as UserInternal | null;
302+
const user = userExtern
303+
? (getModularInstance(userExtern) as UserInternal)
304+
: null;
302305
if (user) {
303306
_assert(
304307
user.auth.config.apiKey === this.config.apiKey,
@@ -556,12 +559,13 @@ export class AuthImpl implements AuthInternal, _FirebaseService {
556559
}
557560

558561
/**
559-
* Method to be used to cast down to our private implmentation of Auth
562+
* Method to be used to cast down to our private implmentation of Auth.
563+
* It will also handle unwrapping from the compat type if necessary
560564
*
561565
* @param auth Auth object passed in from developer
562566
*/
563567
export function _castAuth(auth: Auth): AuthInternal {
564-
return (auth as unknown) as AuthInternal;
568+
return getModularInstance(auth) as AuthInternal;
565569
}
566570

567571
/** Helper class to wrap subscriber logic */

packages-exp/auth-exp/src/core/index.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* limitations under the License.
1616
*/
1717

18+
import { getModularInstance } from '@firebase/util';
1819
import {
1920
Auth,
2021
NextOrObserver,
@@ -53,7 +54,7 @@ export function setPersistence(
5354
auth: Auth,
5455
persistence: Persistence
5556
): Promise<void> {
56-
return auth.setPersistence(persistence);
57+
return getModularInstance(auth).setPersistence(persistence);
5758
}
5859
/**
5960
* Adds an observer for changes to the signed-in user's ID token, which includes sign-in,
@@ -72,7 +73,11 @@ export function onIdTokenChanged(
7273
error?: ErrorFn,
7374
completed?: CompleteFn
7475
): Unsubscribe {
75-
return auth.onIdTokenChanged(nextOrObserver, error, completed);
76+
return getModularInstance(auth).onIdTokenChanged(
77+
nextOrObserver,
78+
error,
79+
completed
80+
);
7681
}
7782
/**
7883
* Adds an observer for changes to the user's sign-in state.
@@ -93,7 +98,11 @@ export function onAuthStateChanged(
9398
error?: ErrorFn,
9499
completed?: CompleteFn
95100
): Unsubscribe {
96-
return auth.onAuthStateChanged(nextOrObserver, error, completed);
101+
return getModularInstance(auth).onAuthStateChanged(
102+
nextOrObserver,
103+
error,
104+
completed
105+
);
97106
}
98107
/**
99108
* Sets the current language to the default device/browser preference.
@@ -103,7 +112,7 @@ export function onAuthStateChanged(
103112
* @public
104113
*/
105114
export function useDeviceLanguage(auth: Auth): void {
106-
auth.useDeviceLanguage();
115+
getModularInstance(auth).useDeviceLanguage();
107116
}
108117
/**
109118
* Asynchronously sets the provided user as {@link Auth.currentUser} on the
@@ -127,7 +136,7 @@ export function updateCurrentUser(
127136
auth: Auth,
128137
user: User | null
129138
): Promise<void> {
130-
return auth.updateCurrentUser(user);
139+
return getModularInstance(auth).updateCurrentUser(user);
131140
}
132141
/**
133142
* Signs out the current user.
@@ -137,7 +146,7 @@ export function updateCurrentUser(
137146
* @public
138147
*/
139148
export function signOut(auth: Auth): Promise<void> {
140-
return auth.signOut();
149+
return getModularInstance(auth).signOut();
141150
}
142151

143152
export { initializeAuth } from './auth/initialize';
@@ -218,5 +227,5 @@ export { reload } from './user/reload';
218227
* @public
219228
*/
220229
export async function deleteUser(user: User): Promise<void> {
221-
return user.delete();
230+
return getModularInstance(user).delete();
222231
}

packages-exp/auth-exp/src/core/strategies/credential.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { _assertLinkedStatus, _link } from '../user/link_unlink';
3030
import { _reauthenticate } from '../user/reauthenticate';
3131
import { UserCredentialImpl } from '../user/user_credential_impl';
3232
import { _castAuth } from '../auth/auth_impl';
33+
import { getModularInstance } from '@firebase/util';
3334

3435
export async function _signInWithCredential(
3536
auth: AuthInternal,
@@ -87,7 +88,7 @@ export async function linkWithCredential(
8788
user: User,
8889
credential: AuthCredential
8990
): Promise<UserCredential> {
90-
const userInternal = user as UserInternal;
91+
const userInternal = getModularInstance(user) as UserInternal;
9192

9293
await _assertLinkedStatus(false, userInternal, credential.providerId);
9394

@@ -110,5 +111,5 @@ export async function reauthenticateWithCredential(
110111
user: User,
111112
credential: AuthCredential
112113
): Promise<UserCredential> {
113-
return _reauthenticate(user as UserInternal, credential);
114+
return _reauthenticate(getModularInstance(user) as UserInternal, credential);
114115
}

packages-exp/auth-exp/src/core/strategies/custom_token.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,11 @@ export async function signInWithCustomToken(
4242
auth: Auth,
4343
customToken: string
4444
): Promise<UserCredential> {
45-
const response: IdTokenResponse = await getIdTokenResponse(auth, {
45+
const authInternal = _castAuth(auth);
46+
const response: IdTokenResponse = await getIdTokenResponse(authInternal, {
4647
token: customToken,
4748
returnSecureToken: true
4849
});
49-
const authInternal = _castAuth(auth);
5050
const cred = await UserCredentialImpl._fromIdTokenResponse(
5151
authInternal,
5252
OperationType.SIGN_IN,

packages-exp/auth-exp/src/core/strategies/email.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import * as api from '../../api/authentication/email_and_password';
3030
import { UserInternal } from '../../model/user';
3131
import { _getCurrentUrl, _isHttpOrHttps } from '../util/location';
3232
import { _setActionCodeSettingsOnRequest } from './action_code_settings';
33+
import { getModularInstance } from '@firebase/util';
3334

3435
/**
3536
* Gets the list of possible sign in methods for the given email address.
@@ -58,7 +59,10 @@ export async function fetchSignInMethodsForEmail(
5859
continueUri
5960
};
6061

61-
const { signinMethods } = await createAuthUri(auth, request);
62+
const { signinMethods } = await createAuthUri(
63+
getModularInstance(auth),
64+
request
65+
);
6266

6367
return signinMethods || [];
6468
}
@@ -97,7 +101,7 @@ export async function sendEmailVerification(
97101
user: User,
98102
actionCodeSettings?: ActionCodeSettings | null
99103
): Promise<void> {
100-
const userInternal = user as UserInternal;
104+
const userInternal = getModularInstance(user) as UserInternal;
101105
const idToken = await user.getIdToken();
102106
const request: api.VerifyEmailRequest = {
103107
requestType: ActionCodeOperation.VERIFY_EMAIL,
@@ -157,7 +161,7 @@ export async function verifyBeforeUpdateEmail(
157161
newEmail: string,
158162
actionCodeSettings?: ActionCodeSettings | null
159163
): Promise<void> {
160-
const userInternal = user as UserInternal;
164+
const userInternal = getModularInstance(user) as UserInternal;
161165
const idToken = await user.getIdToken();
162166
const request: api.VerifyAndChangeEmailRequest = {
163167
requestType: ActionCodeOperation.VERIFY_AND_CHANGE_EMAIL,

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

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import { _setActionCodeSettingsOnRequest } from './action_code_settings';
3535
import { signInWithCredential } from './credential';
3636
import { _castAuth } from '../auth/auth_impl';
3737
import { AuthErrorCode } from '../errors';
38+
import { getModularInstance } from '@firebase/util';
3839

3940
/**
4041
* Sends a password reset email to the given email address.
@@ -73,15 +74,16 @@ export async function sendPasswordResetEmail(
7374
email: string,
7475
actionCodeSettings?: ActionCodeSettings
7576
): Promise<void> {
77+
const authModular = getModularInstance(auth);
7678
const request: authentication.PasswordResetRequest = {
7779
requestType: ActionCodeOperation.PASSWORD_RESET,
7880
email
7981
};
8082
if (actionCodeSettings) {
81-
_setActionCodeSettingsOnRequest(auth, request, actionCodeSettings);
83+
_setActionCodeSettingsOnRequest(authModular, request, actionCodeSettings);
8284
}
8385

84-
await authentication.sendPasswordResetEmail(auth, request);
86+
await authentication.sendPasswordResetEmail(authModular, request);
8587
}
8688

8789
/**
@@ -98,7 +100,7 @@ export async function confirmPasswordReset(
98100
oobCode: string,
99101
newPassword: string
100102
): Promise<void> {
101-
await account.resetPassword(auth, {
103+
await account.resetPassword(getModularInstance(auth), {
102104
oobCode,
103105
newPassword
104106
});
@@ -117,7 +119,7 @@ export async function applyActionCode(
117119
auth: Auth,
118120
oobCode: string
119121
): Promise<void> {
120-
await account.applyActionCode(auth, { oobCode });
122+
await account.applyActionCode(getModularInstance(auth), { oobCode });
121123
}
122124

123125
/**
@@ -134,7 +136,8 @@ export async function checkActionCode(
134136
auth: Auth,
135137
oobCode: string
136138
): Promise<ActionCodeInfo> {
137-
const response = await account.resetPassword(auth, { oobCode });
139+
const authModular = getModularInstance(auth);
140+
const response = await account.resetPassword(authModular, { oobCode });
138141

139142
// Email could be empty only if the request type is EMAIL_SIGNIN or
140143
// VERIFY_AND_CHANGE_EMAIL.
@@ -143,25 +146,25 @@ export async function checkActionCode(
143146
// Multi-factor info could not be empty if the request type is
144147
// REVERT_SECOND_FACTOR_ADDITION.
145148
const operation = response.requestType;
146-
_assert(operation, auth, AuthErrorCode.INTERNAL_ERROR);
149+
_assert(operation, authModular, AuthErrorCode.INTERNAL_ERROR);
147150
switch (operation) {
148151
case ActionCodeOperation.EMAIL_SIGNIN:
149152
break;
150153
case ActionCodeOperation.VERIFY_AND_CHANGE_EMAIL:
151-
_assert(response.newEmail, auth, AuthErrorCode.INTERNAL_ERROR);
154+
_assert(response.newEmail, authModular, AuthErrorCode.INTERNAL_ERROR);
152155
break;
153156
case ActionCodeOperation.REVERT_SECOND_FACTOR_ADDITION:
154-
_assert(response.mfaInfo, auth, AuthErrorCode.INTERNAL_ERROR);
157+
_assert(response.mfaInfo, authModular, AuthErrorCode.INTERNAL_ERROR);
155158
// fall through
156159
default:
157-
_assert(response.email, auth, AuthErrorCode.INTERNAL_ERROR);
160+
_assert(response.email, authModular, AuthErrorCode.INTERNAL_ERROR);
158161
}
159162

160163
// The multi-factor info for revert second factor addition
161164
let multiFactorInfo: MultiFactorInfoImpl | null = null;
162165
if (response.mfaInfo) {
163166
multiFactorInfo = MultiFactorInfoImpl._fromServerResponse(
164-
_castAuth(auth),
167+
_castAuth(authModular),
165168
response.mfaInfo
166169
);
167170
}
@@ -196,7 +199,7 @@ export async function verifyPasswordResetCode(
196199
auth: Auth,
197200
code: string
198201
): Promise<string> {
199-
const { data } = await checkActionCode(auth, code);
202+
const { data } = await checkActionCode(getModularInstance(auth), code);
200203
// Email should always be present since a code was sent to it
201204
return data.email!;
202205
}
@@ -224,7 +227,7 @@ export async function createUserWithEmailAndPassword(
224227
password: string
225228
): Promise<UserCredential> {
226229
const authInternal = _castAuth(auth);
227-
const response = await signUp(auth, {
230+
const response = await signUp(authInternal, {
228231
returnSecureToken: true,
229232
email,
230233
password
@@ -262,7 +265,7 @@ export function signInWithEmailAndPassword(
262265
password: string
263266
): Promise<UserCredential> {
264267
return signInWithCredential(
265-
auth,
268+
getModularInstance(auth),
266269
EmailAuthProvider.credential(email, password)
267270
);
268271
}

packages-exp/auth-exp/src/core/strategies/email_link.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import { _setActionCodeSettingsOnRequest } from './action_code_settings';
3030
import { signInWithCredential } from './credential';
3131
import { AuthErrorCode } from '../errors';
3232
import { _assert } from '../util/assert';
33+
import { getModularInstance } from '@firebase/util';
3334

3435
/**
3536
* Sends a sign-in email link to the user with the specified email.
@@ -63,7 +64,7 @@ import { _assert } from '../util/assert';
6364
* }
6465
* ```
6566
*
66-
* @param auth - The Auth instance.
67+
* @param authInternal - The Auth instance.
6768
* @param email - The user's email address.
6869
* @param actionCodeSettings - The {@link ActionCodeSettings}.
6970
*
@@ -74,20 +75,21 @@ export async function sendSignInLinkToEmail(
7475
email: string,
7576
actionCodeSettings?: ActionCodeSettings
7677
): Promise<void> {
78+
const authModular = getModularInstance(auth);
7779
const request: api.EmailSignInRequest = {
7880
requestType: ActionCodeOperation.EMAIL_SIGNIN,
7981
email
8082
};
8183
_assert(
8284
actionCodeSettings?.handleCodeInApp,
83-
auth,
85+
authModular,
8486
AuthErrorCode.ARGUMENT_ERROR
8587
);
8688
if (actionCodeSettings) {
87-
_setActionCodeSettingsOnRequest(auth, request, actionCodeSettings);
89+
_setActionCodeSettingsOnRequest(authModular, request, actionCodeSettings);
8890
}
8991

90-
await api.sendSignInLinkToEmail(auth, request);
92+
await api.sendSignInLinkToEmail(authModular, request);
9193
}
9294

9395
/**
@@ -145,16 +147,17 @@ export async function signInWithEmailLink(
145147
email: string,
146148
emailLink?: string
147149
): Promise<UserCredential> {
150+
const authModular = getModularInstance(auth);
148151
const credential = EmailAuthProvider.credentialWithLink(
149152
email,
150153
emailLink || _getCurrentUrl()
151154
);
152155
// Check if the tenant ID in the email link matches the tenant ID on Auth
153156
// instance.
154157
_assert(
155-
credential.tenantId === (auth.tenantId || null),
156-
auth,
158+
credential.tenantId === (authModular.tenantId || null),
159+
authModular,
157160
AuthErrorCode.TENANT_ID_MISMATCH
158161
);
159-
return signInWithCredential(auth, credential);
162+
return signInWithCredential(authModular, credential);
160163
}

0 commit comments

Comments
 (0)