Skip to content

Commit f10acb3

Browse files
authored
Update the linkWithCredential API to call the signUp endpoint (#7692)
* Update the linkWithCredential API to call the signUp endpoint * Use the SignUpRequest and Response. * changeset * Add recaptcha enterprise support to the link API (which calls signUp) * Refactored existing recaptcha enterprise tests, added new for link API. mockEndpointWithParams only works for URL params/GET and not for POST body. Removed this usage with a TODO.
1 parent 67c5a90 commit f10acb3

File tree

6 files changed

+370
-117
lines changed

6 files changed

+370
-117
lines changed

.changeset/tiny-items-grin.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@firebase/auth': patch
3+
---
4+
5+
Fixes https://github.com/firebase/firebase-js-sdk/issues/7675

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: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import {
2525
} from '../index';
2626
import { IdTokenResponse } from '../../model/id_token';
2727
import { MfaEnrollment } from './mfa';
28+
import { SignUpRequest, SignUpResponse } from '../authentication/sign_up';
2829

2930
export interface ResetPasswordRequest {
3031
oobCode: string;
@@ -69,6 +70,20 @@ export async function updateEmailPassword(
6970
>(auth, HttpMethod.POST, Endpoint.SET_ACCOUNT_INFO, request);
7071
}
7172

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

packages/auth/src/api/authentication/sign_up.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import { IdTokenResponse } from '../../model/id_token';
2727
import { Auth } from '../../model/public_types';
2828

2929
export interface SignUpRequest {
30+
idToken?: string;
3031
returnSecureToken?: boolean;
3132
email?: string;
3233
password?: string;

0 commit comments

Comments
 (0)