Back to Firebase Js Sdk

app-check package

docs-devsite/app-check.md

12.12.110.6 KB
Original Source

Project: /docs/reference/js/_project.yaml Book: /docs/reference/_book.yaml page_type: reference

{% comment %} DO NOT EDIT THIS FILE! This is generated by the JS SDK team, and any local changes will be overwritten. Changes should be made in the source code at https://github.com/firebase/firebase-js-sdk {% endcomment %}

app-check package

The Firebase App Check Web SDK.

Firebase App Check does not work in a Node.js environment using ReCaptchaV3Provider or ReCaptchaEnterpriseProvider<!-- -->, but can be used in Node.js if you use CustomProvider and write your own attestation method.

Functions

FunctionDescription
<b>function(app, ...)</b>
initializeAppCheck(app, options)Activate App Check for the given app. Can be called only once per app.
<b>function(appCheckInstance, ...)</b>
getLimitedUseToken(appCheckInstance)Requests a Firebase App Check token. This method should be used only if you need to authorize requests to a non-Firebase backend.<!-- -->Returns limited-use tokens that are intended for use with your non-Firebase backend endpoints that are protected with <a href="https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection"> Replay Protection</a>. This method does not affect the token generation behavior of the #getAppCheckToken() method.
getToken(appCheckInstance, forceRefresh)Get the current App Check token. If <code>forceRefresh</code> is false, this function first checks for a valid token in memory, then local persistence (IndexedDB). If not found, or if <code>forceRefresh</code> is true, it makes a request to the App Check endpoint for a fresh token. That request attaches to the most recent in-flight request if one is present.
onTokenChanged(appCheckInstance, observer)Registers a listener to changes in the token state. There can be more than one listener registered at the same time for one or more App Check instances. The listeners call back on the UI thread whenever the current token associated with this App Check instance changes.
onTokenChanged(appCheckInstance, onNext, onError, onCompletion)Registers a listener to changes in the token state. There can be more than one listener registered at the same time for one or more App Check instances. The listeners call back on the UI thread whenever the current token associated with this App Check instance changes.
setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled)Set whether App Check will automatically refresh tokens as needed.

Classes

ClassDescription
CustomProviderCustom provider class.
ReCaptchaEnterpriseProviderApp Check provider that can obtain a reCAPTCHA Enterprise token and exchange it for an App Check token.
ReCaptchaV3ProviderApp Check provider that can obtain a reCAPTCHA V3 token and exchange it for an App Check token.

Interfaces

InterfaceDescription
AppCheckThe Firebase App Check service interface.
AppCheckOptionsOptions for App Check initialization.
AppCheckTokenThe token returned from an App Check provider.
AppCheckTokenResultResult returned by <code>getToken()</code>.
CustomProviderOptionsOptions when creating a CustomProvider<!-- -->.

Type Aliases

Type AliasDescription
AppCheckTokenListenerA listener that is called whenever the App Check token changes.

function(app, ...)

initializeAppCheck(app, options) {:#initializeappcheck_5548dfc}

Activate App Check for the given app. Can be called only once per app.

<b>Signature:</b>

typescript
export declare function initializeAppCheck(app: FirebaseApp | undefined, options: AppCheckOptions): AppCheck;

Parameters

ParameterTypeDescription
appFirebaseApp | undefinedthe FirebaseApp to activate App Check for
optionsAppCheckOptionsApp Check initialization options

<b>Returns:</b>

AppCheck

function(appCheckInstance, ...)

getLimitedUseToken(appCheckInstance) {:#getlimitedusetoken_53ef5e3}

Requests a Firebase App Check token. This method should be used only if you need to authorize requests to a non-Firebase backend.

Returns limited-use tokens that are intended for use with your non-Firebase backend endpoints that are protected with <a href="https://firebase.google.com/docs/app-check/custom-resource-backend#replay-protection"> Replay Protection</a>. This method does not affect the token generation behavior of the #getAppCheckToken() method.

<b>Signature:</b>

typescript
export declare function getLimitedUseToken(appCheckInstance: AppCheck): Promise<AppCheckTokenResult>;

Parameters

ParameterTypeDescription
appCheckInstanceAppCheckThe App Check service instance.

<b>Returns:</b>

Promise<AppCheckTokenResult<!-- -->>

The limited use token.

getToken(appCheckInstance, forceRefresh) {:#gettoken_39fc1b3}

Get the current App Check token. If forceRefresh is false, this function first checks for a valid token in memory, then local persistence (IndexedDB). If not found, or if forceRefresh is true, it makes a request to the App Check endpoint for a fresh token. That request attaches to the most recent in-flight request if one is present.

<b>Signature:</b>

typescript
export declare function getToken(appCheckInstance: AppCheck, forceRefresh?: boolean): Promise<AppCheckTokenResult>;

Parameters

ParameterTypeDescription
appCheckInstanceAppCheckThe App Check service instance.
forceRefreshbooleanIf true, will always try to fetch a fresh token. If false, will use a cached token if found in storage.

<b>Returns:</b>

Promise<AppCheckTokenResult<!-- -->>

onTokenChanged(appCheckInstance, observer) {:#ontokenchanged_9761e16}

Registers a listener to changes in the token state. There can be more than one listener registered at the same time for one or more App Check instances. The listeners call back on the UI thread whenever the current token associated with this App Check instance changes.

<b>Signature:</b>

typescript
export declare function onTokenChanged(appCheckInstance: AppCheck, observer: PartialObserver<AppCheckTokenResult>): Unsubscribe;

Parameters

ParameterTypeDescription
appCheckInstanceAppCheckThe App Check service instance.
observerPartialObserver<!-- --><AppCheckTokenResult<!-- -->>An object with <code>next</code>, <code>error</code>, and <code>complete</code> properties. <code>next</code> is called with an AppCheckTokenResult whenever the token changes. <code>error</code> is optional and is called if an error is thrown by the listener (the <code>next</code> function). <code>complete</code> is unused, as the token stream is unending.

<b>Returns:</b>

Unsubscribe

A function that unsubscribes this listener.

onTokenChanged(appCheckInstance, onNext, onError, onCompletion) {:#ontokenchanged_8ef80a7}

Registers a listener to changes in the token state. There can be more than one listener registered at the same time for one or more App Check instances. The listeners call back on the UI thread whenever the current token associated with this App Check instance changes.

<b>Signature:</b>

typescript
export declare function onTokenChanged(appCheckInstance: AppCheck, onNext: (tokenResult: AppCheckTokenResult) => void, onError?: (error: Error) => void, onCompletion?: () => void): Unsubscribe;

Parameters

ParameterTypeDescription
appCheckInstanceAppCheckThe App Check service instance.
onNext(tokenResult: AppCheckTokenResult<!-- -->) => voidWhen the token changes, this function is called with an AppCheckTokenResult<!-- -->.
onError(error: Error) => voidOptional. Called if there is an error thrown by the listener (the <code>onNext</code> function).
onCompletion() => voidCurrently unused, as the token stream is unending.

<b>Returns:</b>

Unsubscribe

A function that unsubscribes this listener.

setTokenAutoRefreshEnabled(appCheckInstance, isTokenAutoRefreshEnabled) {:#settokenautorefreshenabled_057a76c}

Set whether App Check will automatically refresh tokens as needed.

<b>Signature:</b>

typescript
export declare function setTokenAutoRefreshEnabled(appCheckInstance: AppCheck, isTokenAutoRefreshEnabled: boolean): void;

Parameters

ParameterTypeDescription
appCheckInstanceAppCheckThe App Check service instance.
isTokenAutoRefreshEnabledbooleanIf true, the SDK automatically refreshes App Check tokens as needed. This overrides any value set during <code>initializeAppCheck()</code>.

<b>Returns:</b>

void

AppCheckTokenListener

A listener that is called whenever the App Check token changes.

<b>Signature:</b>

typescript
export type AppCheckTokenListener = (token: AppCheckTokenResult) => void;