Skip to content

Commit

Permalink
Update the linkWithCredential API to call the signUp endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
prameshj committed Oct 11, 2023
1 parent cbfd14c commit 29790a2
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 4 deletions.
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
12 changes: 12 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 @@ -69,6 +69,18 @@ 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: UpdateEmailPasswordRequest
): Promise<UpdateEmailPasswordResponse> {
return _performApiRequest<
UpdateEmailPasswordRequest,
UpdateEmailPasswordResponse
>(auth, HttpMethod.POST, Endpoint.SIGN_UP, request);
}

export interface ApplyActionCodeRequest {
oobCode: string;
tenantId?: string;
Expand Down
4 changes: 2 additions & 2 deletions packages/auth/src/core/credentials/email.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ describe('core/credentials/email', () => {
});

describe('#_linkToIdToken', () => {
it('calls update email password', async () => {
apiMock = mockEndpoint(Endpoint.SET_ACCOUNT_INFO, {
it('calls sign up with email password', async () => {
apiMock = mockEndpoint(Endpoint.SIGN_UP, {
idToken: 'id-token',
refreshToken: 'refresh-token',
expiresIn: '1234',
Expand Down
7 changes: 5 additions & 2 deletions packages/auth/src/core/credentials/email.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@

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

import { updateEmailPassword } from '../../api/account_management/email_and_password';
import {
linkEmailPassword,
updateEmailPassword

Check failure on line 22 in packages/auth/src/core/credentials/email.ts

View workflow job for this annotation

GitHub Actions / Lint

'updateEmailPassword' is defined but never used

Check failure on line 22 in packages/auth/src/core/credentials/email.ts

View workflow job for this annotation

GitHub Actions / Lint

'updateEmailPassword' is defined but never used. Allowed unused vars must match /^_/u
} from '../../api/account_management/email_and_password';
import {
signInWithPassword,
SignInWithPasswordRequest
Expand Down Expand Up @@ -166,7 +169,7 @@ export class EmailAuthCredential extends AuthCredential {
): Promise<IdTokenResponse> {
switch (this.signInMethod) {
case SignInMethod.EMAIL_PASSWORD:
return updateEmailPassword(auth, {
return linkEmailPassword(auth, {
idToken,
returnSecureToken: true,
email: this._email,
Expand Down

0 comments on commit 29790a2

Please sign in to comment.