Skip to content

Commit

Permalink
feat(app-check): adds all web providers as an option (#812)
Browse files Browse the repository at this point in the history
* feat(app-check): adds all web providers as an option

* refactor(app-check): throws error when accessing missing option

* Update .changeset/chilly-squids-hammer.md

Co-authored-by: Robin Genz <[email protected]>

* Update packages/app-check/src/definitions.ts

Co-authored-by: Robin Genz <[email protected]>

* refactor(app-check): adds `Provider` enum

* feat(app-check): allows user to define provider directly

* fix(app-check): fixes linting errors

* Update packages/app-check/src/web.ts

Co-authored-by: Robin Genz <[email protected]>

* refactor(app-check): adds review suggestions

---------

Co-authored-by: Robin Genz <[email protected]>
  • Loading branch information
eljass and robingenz authored Feb 6, 2025
1 parent 7368d01 commit 7f6117e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 20 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-panthers-learn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-firebase/app-check': minor
---

feat(web): support all app check providers
27 changes: 15 additions & 12 deletions packages/app-check/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,16 @@ A working example can be found here: [robingenz/capacitor-firebase-plugin-demo](

## Usage


```typescript
import { FirebaseAppCheck } from '@capacitor-firebase/app-check';
import { ReCaptchaV3Provider } from '@capacitor-firebase/app-check';

const initialize = async () => {
await FirebaseAppCheck.initialize({
provider: new ReCaptchaV3Provider('myKey');
});
};

const getToken = async () => {
const { token } = FirebaseAppCheck.getToken({
Expand All @@ -58,12 +66,6 @@ const getToken = async () => {
return token;
};

const initialize = async () => {
await FirebaseAppCheck.initialize({
siteKey: 'myKey',
});
};

const setTokenAutoRefreshEnabled = async () => {
await FirebaseAppCheck.setTokenAutoRefreshEnabled({ enabled: true });
};
Expand Down Expand Up @@ -207,12 +209,13 @@ Only available for Web.

#### InitializeOptions

| Prop | Type | Description | Default | Since |
| ------------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ------------------ | ----- |
| **`debug`** | <code>boolean</code> | If `true`, the debug provider is used. ⚠️ **Attention**: The debug provider allows access to your Firebase resources from unverified devices. Don't use the debug provider in production builds of your app, and don't share your debug builds with untrusted parties. ⚠️ **Deprecated**: Use `debugToken` instead. This option will be removed in the next major version. Read more: https://firebase.google.com/docs/app-check/web/debug-provider | <code>false</code> | 1.3.0 |
| **`debugToken`** | <code>string \| boolean</code> | If `true`, the debug provider is used. On **Web**, you can also set a predefined debug token string instead of `true`. On Android and iOS, you have to use environment variables for this. ⚠️ **Attention**: The debug provider allows access to your Firebase resources from unverified devices. Don't use the debug provider in production builds of your app, and don't share your debug builds with untrusted parties. | <code>false</code> | 7.1.0 |
| **`isTokenAutoRefreshEnabled`** | <code>boolean</code> | If `true`, the SDK automatically refreshes App Check tokens as needed. | <code>false</code> | 1.3.0 |
| **`siteKey`** | <code>string</code> | The reCAPTCHA v3 site key (public key). Only available for Web. | | 1.3.0 |
| Prop | Type | Description | Default | Since |
| ------------------------------- | ------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------- | ----- |
| **`debug`** | <code>boolean</code> | If `true`, the debug provider is used. ⚠️ **Attention**: The debug provider allows access to your Firebase resources from unverified devices. Don't use the debug provider in production builds of your app, and don't share your debug builds with untrusted parties. ⚠️ **Deprecated**: Use `debugToken` instead. This option will be removed in the next major version. Read more: https://firebase.google.com/docs/app-check/web/debug-provider | <code>false</code> | 1.3.0 |
| **`debugToken`** | <code>string \| boolean</code> | If `true`, the debug provider is used. On **Web**, you can also set a predefined debug token string instead of `true`. On Android and iOS, you have to use environment variables for this. ⚠️ **Attention**: The debug provider allows access to your Firebase resources from unverified devices. Don't use the debug provider in production builds of your app, and don't share your debug builds with untrusted parties. | <code>false</code> | 7.1.0 |
| **`isTokenAutoRefreshEnabled`** | <code>boolean</code> | If `true`, the SDK automatically refreshes App Check tokens as needed. | <code>false</code> | 1.3.0 |
| **`provider`** | <code>any</code> | The provider to use for App Check. Must be an instance of `ReCaptchaV3Provider`, `ReCaptchaEnterpriseProvider`, or `CustomProvider`. Only available for Web. Read more: https://firebase.google.com/docs/app-check/web/custom-provider | <code>ReCaptchaV3Provider</code> | 7.1.0 |
| **`siteKey`** | <code>string</code> | The reCAPTCHA v3 site key (public key). Only available for Web. ⚠️ **Attention**: This option is ignored if `provider` is set. ⚠️ **Deprecated**: Prefer using `provider` option instead. This option will be removed in the next major version. | | 1.3.0 |


#### InstanceFactoryOptions
Expand Down
16 changes: 16 additions & 0 deletions packages/app-check/src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,27 @@ export interface InitializeOptions {
* @default false
*/
isTokenAutoRefreshEnabled?: boolean;
/**
* The provider to use for App Check. Must be an instance of
* `ReCaptchaV3Provider`, `ReCaptchaEnterpriseProvider`, or `CustomProvider`.
*
* Only available for Web.
*
* Read more: https://firebase.google.com/docs/app-check/web/custom-provider
*
* @since 7.1.0
* @default ReCaptchaV3Provider
*/
provider?: any;
/**
* The reCAPTCHA v3 site key (public key).
*
* Only available for Web.
*
* ⚠️ **Attention**: This option is ignored if `provider` is set.
* ⚠️ **Deprecated**: Prefer using `provider` option instead. This option will be removed in the next major version.
*
* @deprecated Use `provider` instead.
* @since 1.3.0
*/
siteKey?: string;
Expand Down
20 changes: 12 additions & 8 deletions packages/app-check/src/web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import {
getToken,
initializeAppCheck,
onTokenChanged,
ReCaptchaV3Provider,
setTokenAutoRefreshEnabled,
} from 'firebase/app-check';

Expand Down Expand Up @@ -62,18 +61,23 @@ export class FirebaseAppCheckWeb
}

public async initialize(options?: InitializeOptions): Promise<void> {
if (!options?.siteKey) {
throw new Error(FirebaseAppCheckWeb.errorSiteKeyMissing);
}
if (options.debugToken) {
if (options?.debugToken) {
self.FIREBASE_APPCHECK_DEBUG_TOKEN = options.debugToken;
} else if (options.debug) {
} else if (options?.debug) {
self.FIREBASE_APPCHECK_DEBUG_TOKEN = true;
}
let provider = options?.provider;
if (!provider) {
if (!options?.siteKey) {
throw new Error(FirebaseAppCheckWeb.errorSiteKeyMissing);
}
const { ReCaptchaV3Provider } = await import('firebase/app-check');
provider = new ReCaptchaV3Provider(options?.siteKey);
}
const app = getApp();
this.appCheckInstance = initializeAppCheck(app, {
provider: new ReCaptchaV3Provider(options.siteKey),
isTokenAutoRefreshEnabled: options.isTokenAutoRefreshEnabled,
provider,
isTokenAutoRefreshEnabled: options?.isTokenAutoRefreshEnabled,
});
}

Expand Down

0 comments on commit 7f6117e

Please sign in to comment.