Ikuti sorotan dari Firebase di Google I/O 2023. Pelajari lebih lanjut

Mengaktifkan penerapan App Check untuk Cloud Functions

Setelah memahami pengaruh App Check terhadap pengguna dan siap melanjutkan, Anda dapat mengaktifkan penerapan App Check.

Untuk mulai menerapkan persyaratan token App Check di Cloud Functions callable, ubah fungsi Anda untuk memeriksa token App Check yang valid, seperti yang ditunjukkan di bawah. Setelah Anda mengaktifkan penerapan, semua permintaan yang belum diverifikasi akan ditolak.

  1. Update dependensi firebase-functions project Anda ke versi 4.0.0 atau yang lebih baru:

    npm install firebase-functions@">=4.0.0"
    

    Selain itu, update dependensi firebase-admin project Anda ke versi 9.8.0 atau yang lebih baru:

    npm install firebase-admin@">=9.8.0"
    
  2. Tetapkan opsi runtime enforceAppCheck untuk fungsi Anda ke true:

    exports.yourCallableFunction = functions.
      .runWith({
        enforceAppCheck: true  // Requests without valid App Check tokens will be rejected.
      })
      .https.onCall((data, context) => {
        // Your function logic follows.
      });
    
  3. Tambahkan pemeriksaan untuk context.app ke fungsi Anda. Fungsi akan gagal jika context.app tidak ditentukan.

    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.
    });
    
  4. Deploy ulang fungsi Anda:

    firebase deploy --only functions
    

Setelah perubahan ini di-deploy, Cloud Functions callable akan mewajibkan token App Check yang valid. SDK klien Cloud Functions secara otomatis melampirkan token App Check saat Anda memanggil fungsi callable.