我根据此处的教程编写了一个 firebase Http 可调用云函数:https : //www.youtube.com/watch?v= 3hj_r_N0qMs from the firebase team。但是,我的函数无法验证用户(我)的自定义声明,因为“context.auth”未定义我已将 firebase、firebase 工具、firebase-functions 和 admin SDK 更新到最新版本。我的函数/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) }) }如果我不尝试访问“上下文”并且我可以向该用户添加自定义声明,则该函数执行良好。但是,如果我尝试访问 context.auth,则会发现错误:Unhandled error TypeError: Cannot read property 'token' of undefined"
添加回答
举报
0/150
提交
取消