-
Notifications
You must be signed in to change notification settings - Fork 2.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(installations): Firebase JS SDK V9 modular API (#7095)
- Loading branch information
Showing
9 changed files
with
320 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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
166 changes: 166 additions & 0 deletions
166
packages/installations/e2e/installations.modular.e2e.js
This file contains 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,166 @@ | ||
/* | ||
* Copyright (c) 2016-present Invertase Limited & Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this library 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. | ||
* | ||
*/ | ||
|
||
const jwt = require('jsonwebtoken'); | ||
|
||
const ID_LENGTH = 22; | ||
const PROJECT_ID = 448618578101; // this is "magic", it's the react-native-firebase-testing project ID | ||
|
||
describe('installations() modular', function () { | ||
describe('firebase v8 compatibility', function () { | ||
describe('getId()', function () { | ||
it('returns a valid installation id', async function () { | ||
const id = await firebase.installations().getId(); | ||
id.should.be.a.String(); | ||
id.length.should.be.equals(ID_LENGTH); | ||
}); | ||
}); | ||
|
||
describe('getToken()', function () { | ||
it('returns a valid auth token with no arguments', async function () { | ||
const id = await firebase.installations().getId(); | ||
const token = await firebase.installations().getToken(); | ||
token.should.be.a.String(); | ||
token.should.not.equal(''); | ||
const decodedToken = jwt.decode(token); | ||
decodedToken.fid.should.equal(id); // fid == firebase installations id | ||
decodedToken.projectNumber.should.equal(PROJECT_ID); | ||
|
||
// token time is "Unix epoch time", which is in seconds vs javascript milliseconds | ||
if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { | ||
return Promise.reject( | ||
new Error('Token already expired: ' + JSON.stringify(decodedToken)), | ||
); | ||
} | ||
|
||
const token2 = await firebase.installations().getToken(true); | ||
token2.should.be.a.String(); | ||
token2.should.not.equal(''); | ||
const decodedToken2 = jwt.decode(token2); | ||
decodedToken2.fid.should.equal(id); | ||
decodedToken2.projectNumber.should.equal(PROJECT_ID); | ||
|
||
// token time is "Unix epoch time", which is in seconds vs javascript milliseconds | ||
if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { | ||
return Promise.reject(new Error('Token already expired')); | ||
} | ||
(token === token2).should.be.false(); | ||
}); | ||
}); | ||
|
||
describe('delete()', function () { | ||
it('successfully deletes', async function () { | ||
const id = await firebase.installations().getId(); | ||
id.should.be.a.String(); | ||
id.length.should.be.equals(ID_LENGTH); | ||
await firebase.installations().delete(); | ||
|
||
// New id should be different | ||
const id2 = await firebase.installations().getId(); | ||
id2.should.be.a.String(); | ||
id2.length.should.be.equals(ID_LENGTH); | ||
(id === id2).should.be.false(); | ||
|
||
const token = await firebase.installations().getToken(false); | ||
const decodedToken = jwt.decode(token); | ||
decodedToken.fid.should.equal(id2); // fid == firebase installations id | ||
|
||
// token time is "Unix epoch time", which is in seconds vs javascript milliseconds | ||
decodedToken.projectNumber.should.equal(PROJECT_ID); | ||
if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { | ||
return Promise.reject(new Error('Token already expired')); | ||
} | ||
}); | ||
}); | ||
}); | ||
|
||
describe('modular', function () { | ||
describe('getId()', function () { | ||
it('returns a valid installation id', async function () { | ||
const { getInstallations, getId } = installationsModular; | ||
const installations = getInstallations(); | ||
|
||
const id = await getId(installations); | ||
id.should.be.a.String(); | ||
id.length.should.be.equals(ID_LENGTH); | ||
}); | ||
}); | ||
|
||
describe('getToken()', function () { | ||
it('returns a valid auth token with no arguments', async function () { | ||
const { getInstallations, getId, getToken } = installationsModular; | ||
const installations = getInstallations(); | ||
|
||
const id = await getId(installations); | ||
const token = await getToken(installations); | ||
token.should.be.a.String(); | ||
token.should.not.equal(''); | ||
const decodedToken = jwt.decode(token); | ||
decodedToken.fid.should.equal(id); // fid == firebase installations id | ||
decodedToken.projectNumber.should.equal(PROJECT_ID); | ||
|
||
// token time is "Unix epoch time", which is in seconds vs javascript milliseconds | ||
if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { | ||
return Promise.reject( | ||
new Error('Token already expired: ' + JSON.stringify(decodedToken)), | ||
); | ||
} | ||
|
||
const token2 = await getToken(installations, true); | ||
token2.should.be.a.String(); | ||
token2.should.not.equal(''); | ||
const decodedToken2 = jwt.decode(token2); | ||
decodedToken2.fid.should.equal(id); | ||
decodedToken2.projectNumber.should.equal(PROJECT_ID); | ||
|
||
// token time is "Unix epoch time", which is in seconds vs javascript milliseconds | ||
if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { | ||
return Promise.reject(new Error('Token already expired')); | ||
} | ||
(token === token2).should.be.false(); | ||
}); | ||
}); | ||
|
||
describe('deleteInstallations()', function () { | ||
it('successfully deletes', async function () { | ||
const { getInstallations, getId, getToken, deleteInstallations } = installationsModular; | ||
const installations = getInstallations(); | ||
|
||
const id = await getId(installations); | ||
id.should.be.a.String(); | ||
id.length.should.be.equals(ID_LENGTH); | ||
await deleteInstallations(installations); | ||
|
||
// New id should be different | ||
const id2 = await getId(installations); | ||
id2.should.be.a.String(); | ||
id2.length.should.be.equals(ID_LENGTH); | ||
(id === id2).should.be.false(); | ||
|
||
const token = await getToken(installations, false); | ||
const decodedToken = jwt.decode(token); | ||
decodedToken.fid.should.equal(id2); // fid == firebase installations id | ||
|
||
// token time is "Unix epoch time", which is in seconds vs javascript milliseconds | ||
decodedToken.projectNumber.should.equal(PROJECT_ID); | ||
if (decodedToken.exp < Math.round(new Date().getTime() / 1000)) { | ||
return Promise.reject(new Error('Token already expired')); | ||
} | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains 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 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 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,42 @@ | ||
import { ReactNativeFirebase } from '@react-native-firebase/app'; | ||
import { FirebaseInstallationsTypes } from '../index'; | ||
|
||
/** | ||
* Returns an instance of Installations associated with the given FirebaseApp instance. | ||
*/ | ||
export declare function getInstallations( | ||
app?: ReactNativeFirebase.FirebaseApp, | ||
): FirebaseInstallationsTypes.Module; | ||
|
||
/** | ||
* Deletes the Firebase Installation and all associated data. | ||
*/ | ||
export declare function deleteInstallations( | ||
installations?: FirebaseInstallationsTypes.Module, | ||
): Promise<void>; | ||
|
||
/** | ||
* Creates a Firebase Installation if there isn't one for the app and returns the Installation ID. | ||
*/ | ||
export declare function getId(installations: FirebaseInstallationsTypes.Module): Promise<string>; | ||
|
||
/** | ||
* Returns a Firebase Installations auth token, identifying the current Firebase Installation. | ||
*/ | ||
export declare function getToken( | ||
installations: FirebaseInstallationsTypes.Module, | ||
forceRefresh?: boolean, | ||
): Promise<string>; | ||
|
||
declare type IdChangeCallbackFn = (installationId: string) => void; | ||
declare type IdChangeUnsubscribeFn = () => void; | ||
|
||
/** | ||
* Throw an error since react-native-firebase does not support this method. | ||
* | ||
* Sets a new callback that will get called when Installation ID changes. Returns an unsubscribe function that will remove the callback when called. | ||
*/ | ||
export declare function onIdChange( | ||
installations: FirebaseInstallationsTypes.Module, | ||
callback: IdChangeCallbackFn, | ||
): IdChangeUnsubscribeFn; |
This file contains 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,64 @@ | ||
/* | ||
* Copyright (c) 2016-present Invertase Limited & Contributors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this library 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 { firebase } from '..'; | ||
|
||
/** | ||
* @param {import("@react-native-firebase/app").ReactNativeFirebase.FirebaseApp} app | ||
* @returns {import("..").FirebaseInstallationsTypes.Module} | ||
*/ | ||
export function getInstallations(app) { | ||
if (app) { | ||
return firebase.app(app.name).installations(); | ||
} | ||
return firebase.app().installations(); | ||
} | ||
|
||
/** | ||
* @param {import("..").FirebaseInstallationsTypes.Module} installations | ||
* @returns {Promise<void>} | ||
*/ | ||
export function deleteInstallations(installations) { | ||
return firebase.app(installations.app.name).installations().delete(); | ||
} | ||
|
||
/** | ||
* @param {import("..").FirebaseInstallationsTypes.Module} installations | ||
* @returns {Promise<string>} | ||
*/ | ||
export function getId(installations) { | ||
return firebase.app(installations.app.name).installations().getId(); | ||
} | ||
|
||
/** | ||
* @param {import("..").FirebaseInstallationsTypes.Module} installations | ||
* @param {boolean | undefined} forceRefresh | ||
* @returns {Promise<string>} | ||
*/ | ||
export function getToken(installations, forceRefresh) { | ||
return firebase.app(installations.app.name).installations().getToken(forceRefresh); | ||
} | ||
|
||
/** | ||
* @param {import("..").FirebaseInstallationsTypes.Module} installations | ||
* @param {(string) => void} callback | ||
* @returns {() => void} | ||
*/ | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
export function onIdChange(installations, callback) { | ||
throw new Error('onIdChange() is unsupported by the React Native Firebase SDK.'); | ||
} |
This file contains 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 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
Oops, something went wrong.