Skip to content
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

feat(app-check): adds all web providers as an option #812

Merged
merged 12 commits into from
Feb 6, 2025
Merged
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