Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update the linkWithCredential API to call the signUp endpoint #7692

Merged
merged 5 commits into from
Nov 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/tiny-items-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@firebase/auth': patch
---

Fixes https://github.com/firebase/firebase-js-sdk/issues/7675
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import * as mockFetch from '../../../test/helpers/mock_fetch';
import { ServerError } from '../errors';
import {
applyActionCode,
linkEmailPassword,
resetPassword,
updateEmailPassword
} from './email_and_password';
Expand Down Expand Up @@ -91,6 +92,65 @@ describe('api/account_management/resetPassword', () => {
});
});

describe('api/account_management/linkEmailPassword', () => {
const request = {
idToken: 'id-token',
returnSecureToken: true,
email: '[email protected]',
password: 'new-password'
};

let auth: TestAuth;

beforeEach(async () => {
auth = await testAuth();
mockFetch.setUp();
});

afterEach(mockFetch.tearDown);

it('should POST to the correct endpoint', async () => {
const mock = mockEndpoint(Endpoint.SIGN_UP, {
idToken: 'id-token'
});

const response = await linkEmailPassword(auth, request);
expect(response.idToken).to.eq('id-token');
expect(mock.calls[0].request).to.eql(request);
expect(mock.calls[0].method).to.eq('POST');
expect(mock.calls[0].headers!.get(HttpHeader.CONTENT_TYPE)).to.eq(
'application/json'
);
expect(mock.calls[0].headers!.get(HttpHeader.X_CLIENT_VERSION)).to.eq(
'testSDK/0.0.0'
);
});

it('should handle errors', async () => {
const mock = mockEndpoint(
Endpoint.SIGN_UP,
{
error: {
code: 400,
message: ServerError.INVALID_EMAIL,
errors: [
{
message: ServerError.INVALID_EMAIL
}
]
}
},
400
);

await expect(linkEmailPassword(auth, request)).to.be.rejectedWith(
FirebaseError,
'Firebase: The email address is badly formatted. (auth/invalid-email).'
);
expect(mock.calls[0].request).to.eql(request);
});
});

describe('api/account_management/updateEmailPassword', () => {
const request = {
idToken: 'id-token',
Expand Down
15 changes: 15 additions & 0 deletions packages/auth/src/api/account_management/email_and_password.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
} from '../index';
import { IdTokenResponse } from '../../model/id_token';
import { MfaEnrollment } from './mfa';
import { SignUpRequest, SignUpResponse } from '../authentication/sign_up';

export interface ResetPasswordRequest {
oobCode: string;
Expand Down Expand Up @@ -69,6 +70,20 @@ export async function updateEmailPassword(
>(auth, HttpMethod.POST, Endpoint.SET_ACCOUNT_INFO, request);
}

// Used for linking an email/password account to an existing idToken. Uses the same request/response
// format as updateEmailPassword.
export async function linkEmailPassword(
auth: Auth,
request: SignUpRequest
): Promise<SignUpResponse> {
return _performApiRequest<SignUpRequest, SignUpResponse>(
auth,
HttpMethod.POST,
Endpoint.SIGN_UP,
request
);
}

export interface ApplyActionCodeRequest {
oobCode: string;
tenantId?: string;
Expand Down
1 change: 1 addition & 0 deletions packages/auth/src/api/authentication/sign_up.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import { IdTokenResponse } from '../../model/id_token';
import { Auth } from '../../model/public_types';

export interface SignUpRequest {
idToken?: string;
returnSecureToken?: boolean;
email?: string;
password?: string;
Expand Down
Loading
Loading