Skip to content

Commit 5149ade

Browse files
committed
Add fetchSignInMethodsForEmail to auth-next
1 parent cb6ef17 commit 5149ade

File tree

3 files changed

+153
-0
lines changed

3 files changed

+153
-0
lines changed
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { FirebaseError } from '@firebase/util';
19+
import { expect, use } from 'chai';
20+
import * as chaiAsPromised from 'chai-as-promised';
21+
import { SinonStub, stub } from 'sinon';
22+
import { mockEndpoint } from '../../../test/api/helper';
23+
import { mockAuth } from '../../../test/mock_auth';
24+
import * as mockFetch from '../../../test/mock_fetch';
25+
import { Endpoint } from '../../api';
26+
import { ServerError } from '../../api/errors';
27+
import { ProviderId } from '../providers';
28+
import * as location from '../util/location';
29+
import { fetchSignInMethodsForEmail } from './email';
30+
31+
use(chaiAsPromised);
32+
33+
describe('fetchSignInMethodsForEmail', () => {
34+
const email = '[email protected]';
35+
const expectedSignInMethods = [ProviderId.PASSWORD, ProviderId.GOOGLE];
36+
37+
beforeEach(mockFetch.setUp);
38+
afterEach(mockFetch.tearDown);
39+
40+
it('should return the sign in methods', async () => {
41+
const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, {
42+
signinMethods: expectedSignInMethods
43+
});
44+
const response = await fetchSignInMethodsForEmail(mockAuth, email);
45+
expect(response).to.eql(expectedSignInMethods);
46+
expect(mock.calls[0].request).to.eql({
47+
identifier: email,
48+
continueUri: location.getCurrentUrl()
49+
});
50+
});
51+
52+
context('on non standard platforms', () => {
53+
let locationStub: SinonStub;
54+
55+
beforeEach(() => {
56+
locationStub = stub(location, 'isHttpOrHttps');
57+
locationStub.callsFake(() => false);
58+
});
59+
60+
afterEach(() => {
61+
locationStub.restore();
62+
});
63+
64+
it('should use localhost for the continueUri', async () => {
65+
const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, {
66+
signinMethods: expectedSignInMethods
67+
});
68+
const response = await fetchSignInMethodsForEmail(mockAuth, email);
69+
expect(response).to.eql(expectedSignInMethods);
70+
expect(mock.calls[0].request).to.eql({
71+
identifier: email,
72+
continueUri: 'http://localhost'
73+
});
74+
});
75+
});
76+
77+
it('should surface errors', async () => {
78+
const mock = mockEndpoint(Endpoint.CREATE_AUTH_URI, {
79+
error: {
80+
code: 400,
81+
message: ServerError.INVALID_EMAIL
82+
}
83+
}, 400);
84+
await expect(fetchSignInMethodsForEmail(mockAuth, email)).to.be.rejectedWith(FirebaseError, 'Firebase: The email address is badly formatted. (auth/invalid-email).');
85+
expect(mock.calls.length).to.eq(1);
86+
});
87+
});
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
import { createAuthUri, CreateAuthUriRequest } from '../../api/authentication/create_auth_uri';
19+
import { Auth } from '../../model/auth';
20+
import { getCurrentUrl, isHttpOrHttps } from '../util/location';
21+
22+
export async function fetchSignInMethodsForEmail(
23+
auth: Auth,
24+
email: string
25+
): Promise<string[]> {
26+
// createAuthUri returns an error if continue URI is not http or https.
27+
// For environments like Cordova, Chrome extensions, native frameworks, file
28+
// systems, etc, use http://localhost as continue URL.
29+
const continueUri = isHttpOrHttps() ? getCurrentUrl() : 'http://localhost';
30+
const request: CreateAuthUriRequest = {
31+
identifier: email,
32+
continueUri
33+
};
34+
35+
const response = await createAuthUri(auth, request);
36+
37+
return response.signinMethods || [];
38+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* @license
3+
* Copyright 2020 Google LLC
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
export function getCurrentUrl(): string {
19+
return window?.location?.href || self?.location?.href || '';
20+
}
21+
22+
export function isHttpOrHttps(): boolean {
23+
return getCurrentScheme() === 'http:' || getCurrentScheme() === 'https:';
24+
}
25+
26+
export function getCurrentScheme(): string | null {
27+
return location?.protocol || null;
28+
}

0 commit comments

Comments
 (0)