เมื่อคุณ เข้าใจว่าการตรวจสอบแอปจะส่งผลต่อผู้ใช้ของคุณอย่างไร และคุณพร้อมที่จะดำเนินการต่อ คุณสามารถเปิดใช้การบังคับใช้การตรวจสอบแอปได้
หากต้องการเริ่มบังคับใช้ข้อกำหนดโทเค็น App Check ใน Cloud Functions ที่เรียกได้ ให้แก้ไขฟังก์ชันเพื่อตรวจสอบโทเค็น 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
เมื่อปรับใช้การเปลี่ยนแปลงเหล่านี้แล้ว ฟังก์ชันคลาวด์ที่เรียกได้ของคุณจะต้องใช้โทเค็น App Check ที่ถูกต้อง SDK ของไคลเอ็นต์ Cloud Functions จะแนบโทเค็นการตรวจสอบแอปโดยอัตโนมัติเมื่อคุณเรียกใช้ฟังก์ชันที่เรียกได้