-
Notifications
You must be signed in to change notification settings - Fork 938
Add initial bare-bones Cordova support / popup redirect #4379
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 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. | ||
*/ | ||
|
||
/** | ||
* This is the file that people using Cordova will actually import. You | ||
* should only include this file if you have something specific about your | ||
* implementation that mandates having a separate entrypoint. Otherwise you can | ||
* just use index.ts | ||
*/ | ||
|
||
import { FirebaseApp } from '@firebase/app-types-exp'; | ||
import { Auth } from '@firebase/auth-types-exp'; | ||
import { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db'; | ||
|
||
import { initializeAuth } from './src'; | ||
import { registerAuth } from './src/core/auth/register'; | ||
import { ClientPlatform } from './src/core/util/version'; | ||
|
||
// Core functionality shared by all clients | ||
export * from './src'; | ||
|
||
// Cordova also supports indexedDB / browserSession / browserLocal | ||
export { indexedDBLocalPersistence } from './src/platform_browser/persistence/indexed_db'; | ||
export { browserLocalPersistence } from './src/platform_browser/persistence/local_storage'; | ||
export { browserSessionPersistence } from './src/platform_browser/persistence/session_storage'; | ||
|
||
export { cordovaPopupRedirectResolver } from './src/platform_cordova/popup_redirect'; | ||
export { signInWithRedirect } from './src/platform_cordova/strategies/redirect'; | ||
|
||
import { cordovaPopupRedirectResolver } from './src/platform_cordova/popup_redirect'; | ||
|
||
export function getAuth(app: FirebaseApp): Auth { | ||
return initializeAuth(app, { | ||
persistence: indexedDBLocalPersistence, | ||
popupRedirectResolver: cordovaPopupRedirectResolver | ||
}); | ||
} | ||
|
||
registerAuth(ClientPlatform.CORDOVA); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 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 * as externs from '@firebase/auth-types-exp'; | ||
import { Auth } from '../../model/auth'; | ||
import { PopupRedirectResolver } from '../../model/popup_redirect'; | ||
import { AuthErrorCode } from '../errors'; | ||
import { _assert } from './assert'; | ||
import { _getInstance } from './instantiator'; | ||
|
||
/** | ||
* Chooses a popup/redirect resolver to use. This prefers the override (which | ||
* is directly passed in), and falls back to the property set on the auth | ||
* object. If neither are available, this function errors w/ an argument error. | ||
* | ||
* @internal | ||
*/ | ||
export function _withDefaultResolver( | ||
auth: Auth, | ||
resolverOverride: externs.PopupRedirectResolver | undefined | ||
): PopupRedirectResolver { | ||
if (resolverOverride) { | ||
return _getInstance(resolverOverride); | ||
} | ||
|
||
_assert(auth._popupRedirectResolver, auth, AuthErrorCode.ARGUMENT_ERROR); | ||
|
||
return auth._popupRedirectResolver; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 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. | ||
*/ | ||
|
||
// For some reason, the linter doesn't recognize that these are used elsewhere | ||
// in the SDK | ||
/* eslint-disable @typescript-eslint/no-unused-vars */ | ||
|
||
declare namespace cordova.plugins.browsertab { | ||
function isAvailable(cb: (available: boolean) => void): void; | ||
function openUrl(url: string): void; | ||
} | ||
|
||
declare namespace cordova.InAppBrowser { | ||
function open(url: string, target: string, options: string): unknown; | ||
} | ||
|
||
declare namespace universalLinks { | ||
function subscribe(n: null, cb: (event: unknown) => void): void; | ||
} | ||
|
||
declare namespace BuildInfo { | ||
const packageName: string; | ||
} |
143 changes: 143 additions & 0 deletions
143
packages-exp/auth-exp/src/platform_cordova/popup_redirect.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
/** | ||
* @license | ||
* Copyright 2021 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 * as chaiAsPromised from 'chai-as-promised'; | ||
import * as sinonChai from 'sinon-chai'; | ||
import { expect, use } from 'chai'; | ||
import { testAuth, TestAuth } from '../../test/helpers/mock_auth'; | ||
import { SingletonInstantiator } from '../core/util/instantiator'; | ||
import { AuthEventType, PopupRedirectResolver } from '../model/popup_redirect'; | ||
import { cordovaPopupRedirectResolver } from './popup_redirect'; | ||
import { GoogleAuthProvider } from '../core/providers/google'; | ||
import { FirebaseError } from '@firebase/util'; | ||
|
||
use(chaiAsPromised); | ||
use(sinonChai); | ||
|
||
describe('platform_cordova/popup_redirect', () => { | ||
let auth: TestAuth; | ||
let resolver: PopupRedirectResolver; | ||
|
||
beforeEach(async () => { | ||
auth = await testAuth(); | ||
attachExpectedPlugins(); | ||
resolver = new (cordovaPopupRedirectResolver as SingletonInstantiator<PopupRedirectResolver>)(); | ||
}); | ||
|
||
describe('_openRedirect plugin checks', () => { | ||
// TODO: Rest of the tests go here | ||
it('does not reject if all plugins installed', () => { | ||
expect(() => | ||
resolver._openRedirect( | ||
auth, | ||
new GoogleAuthProvider(), | ||
AuthEventType.SIGN_IN_VIA_REDIRECT | ||
) | ||
).not.to.throw; | ||
}); | ||
|
||
it('rejects if universal links is missing', () => { | ||
removeProp(window, 'universalLinks'); | ||
expect(() => | ||
resolver._openRedirect( | ||
auth, | ||
new GoogleAuthProvider(), | ||
AuthEventType.SIGN_IN_VIA_REDIRECT | ||
) | ||
) | ||
.to.throw(FirebaseError, 'auth/invalid-cordova-configuration') | ||
.that.has.deep.property('customData', { | ||
appName: 'test-app', | ||
missingPlugin: 'cordova-universal-links-plugin-fix' | ||
}); | ||
}); | ||
|
||
it('rejects if build info is missing', () => { | ||
removeProp(window.BuildInfo, 'packageName'); | ||
expect(() => | ||
resolver._openRedirect( | ||
auth, | ||
new GoogleAuthProvider(), | ||
AuthEventType.SIGN_IN_VIA_REDIRECT | ||
) | ||
) | ||
.to.throw(FirebaseError, 'auth/invalid-cordova-configuration') | ||
.that.has.deep.property('customData', { | ||
appName: 'test-app', | ||
missingPlugin: 'cordova-plugin-buildInfo' | ||
}); | ||
}); | ||
|
||
it('rejects if browsertab openUrl is missing', () => { | ||
removeProp(window.cordova.plugins.browsertab, 'openUrl'); | ||
expect(() => | ||
resolver._openRedirect( | ||
auth, | ||
new GoogleAuthProvider(), | ||
AuthEventType.SIGN_IN_VIA_REDIRECT | ||
) | ||
) | ||
.to.throw(FirebaseError, 'auth/invalid-cordova-configuration') | ||
.that.has.deep.property('customData', { | ||
appName: 'test-app', | ||
missingPlugin: 'cordova-plugin-browsertab' | ||
}); | ||
}); | ||
|
||
it('rejects if InAppBrowser is missing', () => { | ||
removeProp(window.cordova.InAppBrowser, 'open'); | ||
expect(() => | ||
resolver._openRedirect( | ||
auth, | ||
new GoogleAuthProvider(), | ||
AuthEventType.SIGN_IN_VIA_REDIRECT | ||
) | ||
) | ||
.to.throw(FirebaseError, 'auth/invalid-cordova-configuration') | ||
.that.has.deep.property('customData', { | ||
appName: 'test-app', | ||
missingPlugin: 'cordova-plugin-inappbrowser' | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
function attachExpectedPlugins(): void { | ||
// Eventually these will be replaced with full mocks | ||
const win = (window as unknown) as Record<string, unknown>; | ||
win.cordova = { | ||
plugins: { | ||
browsertab: { | ||
isAvailable: () => {}, | ||
openUrl: () => {} | ||
} | ||
}, | ||
InAppBrowser: { | ||
open: () => {} | ||
} | ||
}; | ||
win.universalLinks = { | ||
subscribe: () => {} | ||
}; | ||
win.BuildInfo = { | ||
packageName: 'com.example.name.package' | ||
}; | ||
} | ||
|
||
function removeProp(obj: unknown, prop: string): void { | ||
delete (obj as Record<string, unknown>)[prop]; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need to export this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. It can be used independently (if you use
initializeAuth()
for example instead ofgetAuth()
)