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.
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"
Tetapkan opsi runtime
enforceAppCheck
untuk fungsi Anda ketrue
:exports.yourCallableFunction = functions. .runWith({ enforceAppCheck: true // Requests without valid App Check tokens will be rejected. }) .https.onCall((data, context) => { // Your function logic follows. });
Tambahkan pemeriksaan untuk
context.app
ke fungsi Anda. Fungsi akan gagal jikacontext.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. });
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.