Skip to content

Commit 9190a4c

Browse files
authored
Add fetchSignInMethodsForEmail to auth-next (#2924)
* Add fetchSignInMethodsForEmail to auth-next * [AUTOMATED]: Prettier Code Styling * PR Feedback * [AUTOMATED]: Prettier Code Styling
1 parent bcccf36 commit 9190a4c

File tree

3 files changed

+165
-0
lines changed

3 files changed

+165
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
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(
79+
Endpoint.CREATE_AUTH_URI,
80+
{
81+
error: {
82+
code: 400,
83+
message: ServerError.INVALID_EMAIL
84+
}
85+
},
86+
400
87+
);
88+
await expect(
89+
fetchSignInMethodsForEmail(mockAuth, email)
90+
).to.be.rejectedWith(
91+
FirebaseError,
92+
'Firebase: The email address is badly formatted. (auth/invalid-email).'
93+
);
94+
expect(mock.calls.length).to.eq(1);
95+
});
96+
});
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 {
19+
createAuthUri,
20+
CreateAuthUriRequest
21+
} from '../../api/authentication/create_auth_uri';
22+
import { Auth } from '../../model/auth';
23+
import { getCurrentUrl, isHttpOrHttps } from '../util/location';
24+
25+
export async function fetchSignInMethodsForEmail(
26+
auth: Auth,
27+
email: string
28+
): Promise<string[]> {
29+
// createAuthUri returns an error if continue URI is not http or https.
30+
// For environments like Cordova, Chrome extensions, native frameworks, file
31+
// systems, etc, use http://localhost as continue URL.
32+
const continueUri = isHttpOrHttps() ? getCurrentUrl() : 'http://localhost';
33+
const request: CreateAuthUriRequest = {
34+
identifier: email,
35+
continueUri
36+
};
37+
38+
const { signinMethods } = await createAuthUri(auth, request);
39+
40+
return signinMethods || [];
41+
}
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 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 self?.location?.protocol || null;
28+
}

0 commit comments

Comments
 (0)