Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/afraid-baboons-develop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/app-check': minor
'@firebase/app': minor
---

Default automaticDataCollectionEnabled to true without changing App Check's default behavior.
73 changes: 71 additions & 2 deletions packages/app-check/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,16 +239,85 @@ describe('api', () => {
expect(getStateReference(app).activated).to.equal(true);
});

it('isTokenAutoRefreshEnabled value defaults to global setting', () => {
it('global false + local unset = false', () => {
app.automaticDataCollectionEnabled = false;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('sets isTokenAutoRefreshEnabled correctly, overriding global setting', () => {
it('global false + local true = false', () => {
app.automaticDataCollectionEnabled = false;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: true
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('global false + local false = false', () => {
app.automaticDataCollectionEnabled = false;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: false
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('global unset + local unset = false', () => {
// Global unset should default to true.
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('global unset + local false = false', () => {
// Global unset should default to true.
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: false
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('global unset + local true = true', () => {
// Global unset should default to true.
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: true
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(true);
});

it('global true + local unset = false', () => {
app.automaticDataCollectionEnabled = true;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY)
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('global true + local false = false', () => {
app.automaticDataCollectionEnabled = true;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: false
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(false);
});

it('global true + local true = true', () => {
app.automaticDataCollectionEnabled = true;
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: true
});
expect(getStateReference(app).isTokenAutoRefreshEnabled).to.equal(true);
});

it('sets isTokenAutoRefreshEnabled correctly, overriding global setting', () => {
initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(FAKE_SITE_KEY),
isTokenAutoRefreshEnabled: true
Expand Down
20 changes: 13 additions & 7 deletions packages/app-check/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import {
} from './internal-api';
import { readTokenFromStorage } from './storage';
import { getDebugToken, initializeDebugMode, isDebugMode } from './debug';
import { logger } from './logger';

declare module '@firebase/component' {
interface NameServiceMapping {
Expand Down Expand Up @@ -132,7 +133,7 @@ export function initializeAppCheck(
function _activate(
app: FirebaseApp,
provider: AppCheckProvider,
isTokenAutoRefreshEnabled?: boolean
isTokenAutoRefreshEnabled: boolean = false
): void {
// Create an entry in the APP_CHECK_STATES map. Further changes should
// directly mutate this object.
Expand All @@ -149,13 +150,18 @@ function _activate(
return cachedToken;
});

// Use value of global `automaticDataCollectionEnabled` (which
// itself defaults to false if not specified in config) if
// `isTokenAutoRefreshEnabled` param was not provided by user.
// Global `automaticDataCollectionEnabled` (defaults to true) and
// `isTokenAutoRefreshEnabled` must both be true.
state.isTokenAutoRefreshEnabled =
isTokenAutoRefreshEnabled === undefined
? app.automaticDataCollectionEnabled
: isTokenAutoRefreshEnabled;
isTokenAutoRefreshEnabled && app.automaticDataCollectionEnabled;

if (!app.automaticDataCollectionEnabled && isTokenAutoRefreshEnabled) {
logger.warn(
'`isTokenAutoRefreshEnabled` is true but ' +
'`automaticDataCollectionEnabled` was set to false during' +
' `initializeApp()`. This blocks automatic token refresh.'
);
}

state.provider.initialize(app);
}
Expand Down
19 changes: 12 additions & 7 deletions packages/app/src/api.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,14 +128,14 @@ describe('API tests', () => {
{
apiKey: 'test1'
},
{ automaticDataCollectionEnabled: true }
{ automaticDataCollectionEnabled: false }
);
expect(() =>
initializeApp(
{
apiKey: 'test1'
},
{ automaticDataCollectionEnabled: false }
{ automaticDataCollectionEnabled: true }
)
).throws(/\[DEFAULT\].*exists/i);
});
Expand All @@ -146,14 +146,14 @@ describe('API tests', () => {
{
apiKey: 'test1'
},
{ name: appName, automaticDataCollectionEnabled: true }
{ name: appName, automaticDataCollectionEnabled: false }
);
expect(() =>
initializeApp(
{
apiKey: 'test1'
},
{ name: appName, automaticDataCollectionEnabled: false }
{ name: appName, automaticDataCollectionEnabled: true }
)
).throws(/'MyApp'.*exists/i);
});
Expand All @@ -164,11 +164,16 @@ describe('API tests', () => {
expect(app.name).to.equal(appName);
});

it('sets automaticDataCollectionEnabled', () => {
const app = initializeApp({}, { automaticDataCollectionEnabled: true });
it('sets automaticDataCollectionEnabled to true by default', () => {
const app = initializeApp({});
expect(app.automaticDataCollectionEnabled).to.be.true;
});

it('sets a new automaticDataCollectionEnabled value if provided', () => {
const app = initializeApp({}, { automaticDataCollectionEnabled: false });
expect(app.automaticDataCollectionEnabled).to.be.false;
});

it('adds registered components to App', () => {
_clearComponents();
const comp1 = createTestComponent('test1');
Expand Down Expand Up @@ -211,7 +216,7 @@ describe('API tests', () => {

const app = initializeServerApp(options, serverAppSettings);
expect(app).to.not.equal(null);
expect(app.automaticDataCollectionEnabled).to.be.false;
expect(app.automaticDataCollectionEnabled).to.be.true;
await deleteApp(app);
expect((app as FirebaseServerAppImpl).isDeleted).to.be.true;
});
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ export function initializeApp(

const config: Required<FirebaseAppSettings> = {
name: DEFAULT_ENTRY_NAME,
automaticDataCollectionEnabled: false,
automaticDataCollectionEnabled: true,
...rawConfig
};
const name = config.name;
Expand Down Expand Up @@ -241,7 +241,7 @@ export function initializeServerApp(
}

if (_serverAppConfig.automaticDataCollectionEnabled === undefined) {
_serverAppConfig.automaticDataCollectionEnabled = false;
_serverAppConfig.automaticDataCollectionEnabled = true;
}

let appOptions: FirebaseOptions;
Expand Down
4 changes: 2 additions & 2 deletions packages/app/src/firebaseApp.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ describe('FirebaseAppNext', () => {
};
const app = new FirebaseAppImpl(
options,
{ name: 'test', automaticDataCollectionEnabled: false },
{ name: 'test', automaticDataCollectionEnabled: true },
new ComponentContainer('test')
);

expect(app.automaticDataCollectionEnabled).to.be.false;
expect(app.automaticDataCollectionEnabled).to.be.true;
expect(app.name).to.equal('test');
expect(app.options).to.deep.equal(options);
});
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/firebaseServerApp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ export class FirebaseServerAppImpl
const automaticDataCollectionEnabled =
serverConfig.automaticDataCollectionEnabled !== undefined
? serverConfig.automaticDataCollectionEnabled
: false;
: true;

// Create the FirebaseAppSettings object for the FirebaseAppImp constructor.
const config: Required<FirebaseAppSettings> = {
Expand Down
2 changes: 1 addition & 1 deletion packages/app/src/public-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ export interface FirebaseAppSettings {
*/
name?: string;
/**
* The settable config flag for GDPR opt-in/opt-out
* The settable config flag for GDPR opt-in/opt-out. Defaults to true.
*/
automaticDataCollectionEnabled?: boolean;
}
Expand Down
Loading