Skip to content

Commit 29790a2

Browse files
committed
Update the linkWithCredential API to call the signUp endpoint
1 parent cbfd14c commit 29790a2

File tree

4 files changed

+79
-4
lines changed

4 files changed

+79
-4
lines changed

packages/auth/src/api/account_management/email_and_password.test.ts

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import * as mockFetch from '../../../test/helpers/mock_fetch';
2727
import { ServerError } from '../errors';
2828
import {
2929
applyActionCode,
30+
linkEmailPassword,
3031
resetPassword,
3132
updateEmailPassword
3233
} from './email_and_password';
@@ -91,6 +92,65 @@ describe('api/account_management/resetPassword', () => {
9192
});
9293
});
9394

95+
describe('api/account_management/linkEmailPassword', () => {
96+
const request = {
97+
idToken: 'id-token',
98+
returnSecureToken: true,
99+
100+
password: 'new-password'
101+
};
102+
103+
let auth: TestAuth;
104+
105+
beforeEach(async () => {
106+
auth = await testAuth();
107+
mockFetch.setUp();
108+
});
109+
110+
afterEach(mockFetch.tearDown);
111+
112+
it('should POST to the correct endpoint', async () => {
113+
const mock = mockEndpoint(Endpoint.SIGN_UP, {
114+
idToken: 'id-token'
115+
});
116+
117+
const response = await linkEmailPassword(auth, request);
118+
expect(response.idToken).to.eq('id-token');
119+
expect(mock.calls[0].request).to.eql(request);
120+
expect(mock.calls[0].method).to.eq('POST');
121+
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
122+
'application/json'
123+
);
124+
expect(mock.calls[0].headers!.get(HttpHeader.X_CLIENT_VERSION)).to.eq(
125+
'testSDK/0.0.0'
126+
);
127+
});
128+
129+
it('should handle errors', async () => {
130+
const mock = mockEndpoint(
131+
Endpoint.SIGN_UP,
132+
{
133+
error: {
134+
code: 400,
135+
message: ServerError.INVALID_EMAIL,
136+
errors: [
137+
{
138+
message: ServerError.INVALID_EMAIL
139+
}
140+
]
141+
}
142+
},
143+
400
144+
);
145+
146+
await expect(linkEmailPassword(auth, request)).to.be.rejectedWith(
147+
FirebaseError,
148+
'Firebase: The email address is badly formatted. (auth/invalid-email).'
149+
);
150+
expect(mock.calls[0].request).to.eql(request);
151+
});
152+
});
153+
94154
describe('api/account_management/updateEmailPassword', () => {
95155
const request = {
96156
idToken: 'id-token',

packages/auth/src/api/account_management/email_and_password.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,18 @@ export async function updateEmailPassword(
6969
>(auth, HttpMethod.POST, Endpoint.SET_ACCOUNT_INFO, request);
7070
}
7171

72+
// Used for linking an email/password account to an existing idToken. Uses the same request/response
73+
// format as updateEmailPassword.
74+
export async function linkEmailPassword(
75+
auth: Auth,
76+
request: UpdateEmailPasswordRequest
77+
): Promise<UpdateEmailPasswordResponse> {
78+
return _performApiRequest<
79+
UpdateEmailPasswordRequest,
80+
UpdateEmailPasswordResponse
81+
>(auth, HttpMethod.POST, Endpoint.SIGN_UP, request);
82+
}
83+
7284
export interface ApplyActionCodeRequest {
7385
oobCode: string;
7486
tenantId?: string;

packages/auth/src/core/credentials/email.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -288,8 +288,8 @@ describe('core/credentials/email', () => {
288288
});
289289

290290
describe('#_linkToIdToken', () => {
291-
it('calls update email password', async () => {
292-
apiMock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
291+
it('calls sign up with email password', async () => {
292+
apiMock = mockEndpoint(Endpoint.SIGN_UP, {
293293
idToken: 'id-token',
294294
refreshToken: 'refresh-token',
295295
expiresIn: '1234',

packages/auth/src/core/credentials/email.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@
1717

1818
import { ProviderId, SignInMethod } from '../../model/enums';
1919

20-
import { updateEmailPassword } from '../../api/account_management/email_and_password';
20+
import {
21+
linkEmailPassword,
22+
updateEmailPassword
23+
} from '../../api/account_management/email_and_password';
2124
import {
2225
signInWithPassword,
2326
SignInWithPasswordRequest
@@ -166,7 +169,7 @@ export class EmailAuthCredential extends AuthCredential {
166169
): Promise<IdTokenResponse> {
167170
switch (this.signInMethod) {
168171
case SignInMethod.EMAIL_PASSWORD:
169-
return updateEmailPassword(auth, {
172+
return linkEmailPassword(auth, {
170173
idToken,
171174
returnSecureToken: true,
172175
email: this._email,

0 commit comments

Comments
 (0)