-
Notifications
You must be signed in to change notification settings - Fork 938
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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); | ||
}); | ||
}); |
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 response = await createAuthUri(auth, request); | ||
|
||
return response.signinMethods || []; | ||
} |
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 window?.location?.href || self?.location?.href || ''; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Isn't self just a direct alias to window? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. apparently window doesn't exist in workers but self does, this should probably just use the self version though There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
} | ||
|
||
export function isHttpOrHttps(): boolean { | ||
return getCurrentScheme() === 'http:' || getCurrentScheme() === 'https:'; | ||
} | ||
|
||
export function getCurrentScheme(): string | null { | ||
return location?.protocol || null; | ||
} |
Uh oh!
There was an error while loading. Please reload this page.