Firebase云功能-权限被拒绝

时间:2018-11-18 19:08:56

标签: node.js firebase firebase-realtime-database google-cloud-functions firebase-admin

我正以管理员身份面对Firebase函数并遇到问题。

根据此https://firebase.google.com/docs/functions/beta-v1-diff#realtimedatabase adminRef替换为ref

然后我使用change.after.ref,但仍然出现Permission denied错误。

我做错了什么?我只想在/stripe_customers/QymIMpC0ZccSG5mGdFFMdgBksFq1/customer_id

上设置一个值

我不确定是否已正确设置process.env.FIREBASE_CONFIG

exports.createStripeCustomerManual =  functions.database.ref('/Users/{userID}').onUpdate((change, context) => {

    // const beforeData = change.before.val(); // data before the update
    const afterData = change.after.val(); // data after the update
    const userID = context.params.userID;
    const email = afterData.email;
    console.log('createStripeCustomer', afterData, userID, email);

    const appOptions = JSON.parse(process.env.FIREBASE_CONFIG);
    appOptions.databaseAuthVariableOverride = context.auth;
    const appCSCM = admin.initializeApp(appOptions, 'appCSCM');

    const deleteApp = () => appCSCM.delete().catch(() => null);

    return appCSCM.database().ref("/stripe_customers/"+userID+"/customer_id")
            .set("Firebase please DO IT AS AN ADMIN!!!!").then(res => {
                // Deleting the app is necessary for preventing concurrency leaks
                console.log('stripe user created!');
                return deleteApp().then(() => res);
            })
            .catch(err => {
                console.log('ERROR cscm: ', err);
                return deleteApp().then(() => Promise.reject(err));
            });

});

package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "stripe": "^4.15.0",
    "firebase-admin": "~6.0.0",
    "firebase-functions": "^2.1.0"
  },
  "private": true
}

enter image description here

错误:

[2018-11-18T22:16:07.035Z]  @firebase/database: FIREBASE WARNING: set at /stripe_customers/QymIMpC0ZccSG5mGdFFMdgBksFq1/customer_id failed: permission_denied 


Error: PERMISSION_DENIED: Permission denied
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:13080:36
at exceptionGuard (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:691:9)
at Repo.callOnCompleteCallback (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:13071:13)
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12848:19
at /user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12023:17
at PersistentConnection.onDataMessage_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:12056:17)
at Connection.onDataMessage_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:11341:14)
at Connection.onPrimaryMessageReceived_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:11335:18)
at WebSocketConnection.onMessage (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:11236:27)
at WebSocketConnection.appendFrame_ (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:10841:18)
at WebSocketConnection.handleIncomingFrame (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:10891:22)
at Client.mySock.onmessage (/user_code/node_modules/firebase-admin/node_modules/@firebase/database/dist/index.node.cjs.js:10788:19)
at Client.dispatchEvent (/user_code/node_modules/firebase-admin/node_modules/faye-websocket/lib/faye/websocket/api/event_target.js:22:30)
at Client._receiveMessage (/user_code/node_modules/firebase-admin/node_modules/faye-websocket/lib/faye/websocket/api.js:150:10)
at Client.<anonymous> (/user_code/node_modules/firebase-admin/node_modules/faye-websocket/lib/faye/websocket/api.js:34:49)
at emitOne (events.js:101:20)

0 个答案:

没有答案