App Check がユーザーに与える影響を理解し、続行する準備ができたら、App Check の適用を有効にできます。
呼び出し可能な Cloud Functions の関数で App Check トークンを必須にするには、以下に示すように、有効な App Check トークンを確認するように関数を変更します。適用を有効にすると、未検証のリクエストはすべて拒否されます。
プロジェクトの
firebase-functions
依存関係をバージョン 4.0.0 以降に更新します。npm install firebase-functions@">=4.0.0"
プロジェクトの
firebase-admin
依存関係をバージョン 9.8.0 以降に更新します。npm install firebase-admin@">=9.8.0"
関数の
enforceAppCheck
ランタイム オプションをtrue
に設定します。exports.yourCallableFunction = functions. .runWith({ enforceAppCheck: true // Requests without valid App Check tokens will be rejected. }) .https.onCall((data, context) => { // Your function logic follows. });
関数に
context.app
のチェックを追加します。context.app
が定義されていない場合、関数は失敗します。exports.yourCallableFunction = functions.https.onCall((data, context) => { // context.app will be undefined if the request doesn't include an // App Check token. (If the request includes an invalid App Check // token, the request will be rejected with HTTP error 401.) if (context.app == undefined) { throw new functions.https.HttpsError( 'failed-precondition', 'The function must be called from an App Check verified app.') } // Your function logic follows. });
関数を再デプロイします。
firebase deploy --only functions
これらの変更がデプロイされると、呼び出し可能な Cloud Functions の関数で有効な App Check トークンが必須になります。呼び出し可能関数を呼び出すと、Cloud Functions クライアントの SDK が App Check トークンを自動的に追加します。