我根據(jù)此處的教程編寫了一個 firebase Http 可調用云函數(shù):https : //www.youtube.com/watch?v= 3hj_r_N0qMs from the firebase team。但是,我的函數(shù)無法驗證用戶(我)的自定義聲明,因為“context.auth”未定義我已將 firebase、firebase 工具、firebase-functions 和 admin SDK 更新到最新版本。我的函數(shù)/Index.ts 文件import * as functions from 'firebase-functions';import * as admin from 'firebase-admin';admin.initializeApp()export const addAdmin = functions.https.onCall((data, context) => { if (context.auth.token.admin !== true) { return { error: 'Request not authorized' }; } const uid = data.uid return grantAdminRole(uid).then(() => { return { result: `Request fulfilled!` } })})async function grantAdminRole(uid: string): Promise<void> { const user = await admin.auth().getUser(uid); if (user.customClaims && (user.customClaims as any).admin === true) { console.log('already admin') return; } return admin.auth().setCustomUserClaims(user.uid, { admin: true, }).then(() => { console.log('made admin'); })}我的 app.component.ts 代碼makeAdmin() { var addAdmin = firebase.functions().httpsCallable('addAdmin'); addAdmin({ uid: '[MY-USER-ID]' }).then(res => { console.log(res); }) .catch(error => { console.log(error) }) }如果我不嘗試訪問“上下文”并且我可以向該用戶添加自定義聲明,則該函數(shù)執(zhí)行良好。但是,如果我嘗試訪問 context.auth,則會發(fā)現(xiàn)錯誤:Unhandled error TypeError: Cannot read property 'token' of undefined"
Firebase 可調用函數(shù)上下文未定義
胡子哥哥
2021-10-14 17:15:09