Skip to content

Add fetchSignInMethodsForEmail to auth-next #2924

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

Merged
merged 4 commits into from
Apr 17, 2020
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
96 changes: 96 additions & 0 deletions packages-exp/auth-exp/src/core/strategies/email.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import { FirebaseError } from '@firebase/util';
import { expect, use } from 'chai';
import * as chaiAsPromised from 'chai-as-promised';
import { SinonStub, stub } from 'sinon';
import { mockEndpoint } from '../../../test/api/helper';
import { mockAuth } from '../../../test/mock_auth';
import * as mockFetch from '../../../test/mock_fetch';
import { Endpoint } from '../../api';
import { ServerError } from '../../api/errors';
import { ProviderId } from '../providers';
import * as location from '../util/location';
import { fetchSignInMethodsForEmail } from './email';

use(chaiAsPromised);

describe('fetchSignInMethodsForEmail', () => {
const email = '[email protected]';
const expectedSignInMethods = [ProviderId.PASSWORD, ProviderId.GOOGLE];

beforeEach(mockFetch.setUp);
afterEach(mockFetch.tearDown);

it('should return the sign in methods', async () => {
const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, {
signinMethods: expectedSignInMethods
});
const response = await fetchSignInMethodsForEmail(mockAuth, email);
expect(response).to.eql(expectedSignInMethods);
expect(mock.calls[0].request).to.eql({
identifier: email,
continueUri: location.getCurrentUrl()
});
});

context('on non standard platforms', () => {
let locationStub: SinonStub;

beforeEach(() => {
locationStub = stub(location, 'isHttpOrHttps');
locationStub.callsFake(() => false);
});

afterEach(() => {
locationStub.restore();
});

it('should use localhost for the continueUri', async () => {
const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, {
signinMethods: expectedSignInMethods
});
const response = await fetchSignInMethodsForEmail(mockAuth, email);
expect(response).to.eql(expectedSignInMethods);
expect(mock.calls[0].request).to.eql({
identifier: email,
continueUri: 'http://localhost'
});
});
});

it('should surface errors', async () => {
const mock = mockEndpoint(
Endpoint.CREATE_AUTH_URI,
{
error: {
code: 400,
message: ServerError.INVALID_EMAIL
}
},
400
);
await expect(
fetchSignInMethodsForEmail(mockAuth, email)
).to.be.rejectedWith(
FirebaseError,
'Firebase: The email address is badly formatted. (auth/invalid-email).'
);
expect(mock.calls.length).to.eq(1);
});
});
41 changes: 41 additions & 0 deletions packages-exp/auth-exp/src/core/strategies/email.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import {
createAuthUri,
CreateAuthUriRequest
} from '../../api/authentication/create_auth_uri';
import { Auth } from '../../model/auth';
import { getCurrentUrl, isHttpOrHttps } from '../util/location';

export async function fetchSignInMethodsForEmail(
auth: Auth,
email: string
): Promise<string[]> {
// createAuthUri returns an error if continue URI is not http or https.
// For environments like Cordova, Chrome extensions, native frameworks, file
// systems, etc, use http://localhost as continue URL.
const continueUri = isHttpOrHttps() ? getCurrentUrl() : 'http://localhost';
const request: CreateAuthUriRequest = {
identifier: email,
continueUri
};

const { signinMethods } = await createAuthUri(auth, request);

return signinMethods || [];
}
28 changes: 28 additions & 0 deletions packages-exp/auth-exp/src/core/util/location.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/**
* @license
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

export function getCurrentUrl(): string {
return self?.location?.href || '';
}

export function isHttpOrHttps(): boolean {
return getCurrentScheme() === 'http:' || getCurrentScheme() === 'https:';
}

export function getCurrentScheme(): string | null {
return self?.location?.protocol || null;
}