Skip to content

Commit d323eb2

Browse files
sam-gcavolkovi
authored andcommitted
Refactor the way UserCredentials work in anticipation of mudularization (#3489)
* Refactor the way UserCredentials work in anticipation of mudularization * Formatting * PR feedback * Linter fixes * Formatting
1 parent 958f3b8 commit d323eb2

21 files changed

+336
-292
lines changed

packages-exp/auth-exp/src/core/credentials/from_token_response.test.ts

Lines changed: 0 additions & 43 deletions
This file was deleted.

packages-exp/auth-exp/src/core/credentials/from_token_response.ts

Lines changed: 0 additions & 36 deletions
This file was deleted.

packages-exp/auth-exp/src/core/providers/phone.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,18 @@
1616
*/
1717

1818
import * as externs from '@firebase/auth-types-exp';
19-
2019
// eslint-disable-next-line import/no-extraneous-dependencies
2120
import { FirebaseError } from '@firebase/util';
21+
22+
import { SignInWithPhoneNumberResponse } from '../../api/authentication/sms';
23+
import { ApplicationVerifier } from '../../model/application_verifier';
2224
import { Auth } from '../../model/auth';
25+
import { UserCredential } from '../../model/user';
2326
import { initializeAuth } from '../auth/auth_impl';
2427
import { PhoneAuthCredential } from '../credentials/phone';
28+
import { AuthErrorCode } from '../errors';
2529
import { _verifyPhoneNumber } from '../strategies/phone';
26-
import { debugFail } from '../util/assert';
27-
import { ApplicationVerifier } from '../../model/application_verifier';
30+
import { assert, debugFail, fail } from '../util/assert';
2831

2932
export class PhoneAuthProvider implements externs.PhoneAuthProvider {
3033
static readonly PROVIDER_ID = externs.ProviderId.PHONE;
@@ -58,11 +61,27 @@ export class PhoneAuthProvider implements externs.PhoneAuthProvider {
5861
);
5962
}
6063

61-
static _credentialFromResult(
64+
static credentialFromResult(
6265
userCredential: externs.UserCredential
6366
): externs.AuthCredential | null {
64-
void userCredential;
65-
return debugFail('not implemented');
67+
const credential = userCredential as UserCredential;
68+
assert(
69+
credential._tokenResponse,
70+
credential.user.auth.name,
71+
AuthErrorCode.ARGUMENT_ERROR
72+
);
73+
const {
74+
phoneNumber,
75+
temporaryProof
76+
} = credential._tokenResponse as SignInWithPhoneNumberResponse;
77+
if (phoneNumber && temporaryProof) {
78+
return PhoneAuthCredential._fromTokenResponse(
79+
phoneNumber,
80+
temporaryProof
81+
);
82+
}
83+
84+
fail(credential.user.auth.name, AuthErrorCode.ARGUMENT_ERROR);
6685
}
6786

6887
static _credentialFromError(

packages-exp/auth-exp/src/core/strategies/abstract_popup_redirect_operation.test.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ import * as chaiAsPromised from 'chai-as-promised';
2020
import * as sinon from 'sinon';
2121
import * as sinonChai from 'sinon-chai';
2222

23-
import { OperationType } from '@firebase/auth-types-exp';
23+
import { OperationType, ProviderId } from '@firebase/auth-types-exp';
2424
import { FirebaseError } from '@firebase/util';
2525

2626
import { delay } from '../../../test/helpers/delay';
27+
import { TEST_ID_TOKEN_RESPONSE } from '../../../test/helpers/id_token_response';
2728
import { authEvent, BASE_AUTH_EVENT } from '../../../test/helpers/iframe_event';
2829
import { testAuth, testUser } from '../../../test/helpers/mock_auth';
2930
import { makeMockPopupRedirectResolver } from '../../../test/helpers/mock_popup_redirect_resolver';
@@ -85,11 +86,12 @@ describe('src/core/strategies/abstract_popup_redirect_operation', () => {
8586
);
8687
idpStubs._signIn.returns(
8788
Promise.resolve(
88-
new UserCredentialImpl(
89-
testUser(auth, 'uid'),
90-
null,
91-
OperationType.SIGN_IN
92-
)
89+
new UserCredentialImpl({
90+
user: testUser(auth, 'uid'),
91+
providerId: ProviderId.GOOGLE,
92+
_tokenResponse: { ...TEST_ID_TOKEN_RESPONSE },
93+
operationType: OperationType.SIGN_IN
94+
})
9395
)
9496
);
9597
});
@@ -146,7 +148,7 @@ describe('src/core/strategies/abstract_popup_redirect_operation', () => {
146148
finishPromise(authEvent());
147149
const cred = (await operation.execute())!;
148150
expect(cred.user.uid).to.eq('uid');
149-
expect(cred.credential).to.be.null;
151+
expect(cred._tokenResponse).to.eql(TEST_ID_TOKEN_RESPONSE);
150152
expect(cred.operationType).to.eq(OperationType.SIGN_IN);
151153
});
152154

packages-exp/auth-exp/src/core/strategies/anonymous.test.ts

Lines changed: 4 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@
1717

1818
import { expect } from 'chai';
1919

20-
import {
21-
OperationType,
22-
ProviderId,
23-
SignInMethod
24-
} from '@firebase/auth-types-exp';
20+
import { OperationType } from '@firebase/auth-types-exp';
2521

2622
import { mockEndpoint } from '../../../test/helpers/api/helper';
2723
import { testAuth, testUser } from '../../../test/helpers/mock_auth';
@@ -54,9 +50,7 @@ describe('core/strategies/anonymous', () => {
5450

5551
describe('signInAnonymously', () => {
5652
it('should sign in an anonymous user', async () => {
57-
const { credential, user, operationType } = await signInAnonymously(auth);
58-
expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS);
59-
expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS);
53+
const { user, operationType } = await signInAnonymously(auth);
6054
expect(operationType).to.eq(OperationType.SIGN_IN);
6155
expect(user.uid).to.eq(serverUser.localId);
6256
expect(user.isAnonymous).to.be.true;
@@ -67,11 +61,7 @@ describe('core/strategies/anonymous', () => {
6761
const userCredential = await signInAnonymously(auth);
6862
expect(userCredential.user.isAnonymous).to.be.true;
6963

70-
const { credential, user, operationType } = await signInAnonymously(
71-
auth
72-
);
73-
expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS);
74-
expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS);
64+
const { user, operationType } = await signInAnonymously(auth);
7565
expect(operationType).to.eq(OperationType.SIGN_IN);
7666
expect(user.uid).to.eq(userCredential.user.uid);
7767
expect(user.isAnonymous).to.be.true;
@@ -84,11 +74,7 @@ describe('core/strategies/anonymous', () => {
8474
await auth.updateCurrentUser(fakeUser);
8575
expect(fakeUser.isAnonymous).to.be.false;
8676

87-
const { credential, user, operationType } = await signInAnonymously(
88-
auth
89-
);
90-
expect(credential?.providerId).to.eq(ProviderId.ANONYMOUS);
91-
expect(credential?.signInMethod).to.eq(SignInMethod.ANONYMOUS);
77+
const { user, operationType } = await signInAnonymously(auth);
9278
expect(operationType).to.eq(OperationType.SIGN_IN);
9379
expect(user.uid).to.not.eq(fakeUser.uid);
9480
expect(user.isAnonymous).to.be.true;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
*/
1717

1818
import * as externs from '@firebase/auth-types-exp';
19+
1920
import { Auth } from '../../model/auth';
2021
import { AnonymousProvider } from '../providers/anonymous';
2122
import { UserCredentialImpl } from '../user/user_credential_impl';
@@ -28,11 +29,11 @@ export async function signInAnonymously(
2829
const credential = AnonymousProvider.credential();
2930
if (auth.currentUser?.isAnonymous) {
3031
// If an anonymous user is already signed in, no need to sign them in again.
31-
return new UserCredentialImpl(
32-
auth.currentUser,
33-
credential,
34-
externs.OperationType.SIGN_IN
35-
);
32+
return new UserCredentialImpl({
33+
user: auth.currentUser,
34+
providerId: null,
35+
operationType: externs.OperationType.SIGN_IN
36+
});
3637
}
3738
return signInWithCredential(auth, credential);
3839
}

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

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ import { IdTokenMfaResponse } from '../../api/authentication/mfa';
3737
import { MultiFactorError } from '../../mfa/mfa_error';
3838
import { Auth } from '../../model/auth';
3939
import { IdTokenResponse, IdTokenResponseKind } from '../../model/id_token';
40-
import { User } from '../../model/user';
40+
import { User, UserCredential } from '../../model/user';
4141
import { AuthCredential } from '../credentials';
4242
import { AUTH_ERROR_FACTORY, AuthErrorCode } from '../errors';
4343
import {
@@ -95,12 +95,11 @@ describe('core/strategies/credential', () => {
9595
stub(authCredential, '_getIdTokenResponse').returns(
9696
Promise.resolve(idTokenResponse)
9797
);
98-
const { credential, user, operationType } = await signInWithCredential(
98+
const { user, operationType, ...rest } = await signInWithCredential(
9999
auth,
100100
authCredential
101101
);
102-
expect(credential!.providerId).to.eq(ProviderId.FIREBASE);
103-
expect(credential!.signInMethod).to.eq(SignInMethod.EMAIL_LINK);
102+
expect((rest as UserCredential)._tokenResponse).to.eq(idTokenResponse);
104103
expect(user.uid).to.eq('local-id');
105104
expect(user.tenantId).to.eq('tenant-id');
106105
expect(user.displayName).to.eq('display-name');
@@ -171,13 +170,16 @@ describe('core/strategies/credential', () => {
171170
);
172171

173172
const {
174-
credential,
175173
user: newUser,
176-
operationType
174+
operationType,
175+
...rest
177176
} = await reauthenticateWithCredential(user, authCredential);
178177
expect(operationType).to.eq(OperationType.REAUTHENTICATE);
179178
expect(newUser).to.eq(user);
180-
expect(credential).to.be.null;
179+
expect((rest as UserCredential)._tokenResponse).to.eql({
180+
...idTokenResponse,
181+
idToken: makeJWT({ sub: 'uid' })
182+
});
181183
});
182184
});
183185

@@ -206,13 +208,13 @@ describe('core/strategies/credential', () => {
206208
Promise.resolve(idTokenResponse)
207209
);
208210
const {
209-
credential,
210211
user: newUser,
211-
operationType
212+
operationType,
213+
...rest
212214
} = await linkWithCredential(user, authCredential);
213215
expect(operationType).to.eq(OperationType.LINK);
214216
expect(newUser).to.eq(user);
215-
expect(credential).to.be.null;
217+
expect((rest as UserCredential)._tokenResponse).to.eq(idTokenResponse);
216218
});
217219
});
218220
});

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717

1818
import * as externs from '@firebase/auth-types-exp';
19-
2019
import { OperationType, UserCredential } from '@firebase/auth-types-exp';
20+
2121
import { _processCredentialSavingMfaContextIfNecessary } from '../../mfa/mfa_error';
2222
import { Auth } from '../../model/auth';
2323
import { User } from '../../model/user';

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

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { Endpoint } from '../../api';
2727
import { APIUserInfo } from '../../api/account_management/account';
2828
import { Auth } from '../../model/auth';
2929
import { IdTokenResponse, IdTokenResponseKind } from '../../model/id_token';
30+
import { UserCredential } from '../../model/user';
3031
import { signInWithCustomToken } from './custom_token';
3132

3233
use(chaiAsPromised);
@@ -65,11 +66,15 @@ describe('core/strategies/signInWithCustomToken', () => {
6566
afterEach(mockFetch.tearDown);
6667

6768
it('should return a valid user credential', async () => {
68-
const { credential, user, operationType } = await signInWithCustomToken(
69+
const {
70+
user,
71+
operationType,
72+
_tokenResponse
73+
} = (await signInWithCustomToken(
6974
auth,
7075
'look-at-me-im-a-jwt'
71-
);
72-
expect(credential).to.be.null;
76+
)) as UserCredential;
77+
expect(_tokenResponse).to.eql(idTokenResponse);
7378
expect(user.uid).to.eq('local-id');
7479
expect(user.tenantId).to.eq('tenant-id');
7580
expect(user.displayName).to.eq('display-name');

0 commit comments

Comments
 (0)