我的问题如下:我有一个系统,该系统具有一些资产信息(例如一本书),并且该资产具有一些特征(例如标题,当前所有者,颜色)。我希望能够仅通过执行引言中介绍的HTTP请求来更改当前所有者:https://firebase.google.com/docs/functions/get-started
乍一看似乎很简单,但是例如,我无法获得当前的用户ID或浏览器会话电子邮件。我尝试过使用实时数据库触发器来寻找解决方案:https://firebase.google.com/docs/functions/database-events
我下面的当前代码:
const functions = require('firebase-functions');
// // Create and Deploy Your First Cloud Functions
// // https://firebase.google.com/docs/functions/write-firebase-functions
//
// exports.helloWorld = functions.https.onRequest((request, response) => {
// response.send("Hello from Firebase!");
// });
// The Firebase Admin SDK to access the Firebase Realtime Database.
const admin = require('firebase-admin');
admin.initializeApp();
//const user = admin.auth.user();
function dir(object) {
stuff = [];
for (s in object) {
stuff.push(s);
}
stuff.sort();
return stuff;
}
// Take the text parameter passed to this HTTP endpoint and insert it into the
// Realtime Database under the path /messages/:pushId/original
exports.addMessage = functions.https.onRequest(async (req, res) => {
// Grab the text parameter.
const original = req.query.text;
// Push the new message into the Realtime Database using the Firebase Admin SDK.
const snapshot = await admin.database().ref('/messages').push({original: original});
//const snapshot2 = await admin.database().ref('/messages').push({original: email});
// Redirect with 303 SEE OTHER to the URL of the pushed object in the Firebase console.
res.redirect(303, snapshot.ref.toString());
});
exports.impersonateMakeUpperCase = functions.database.ref('/messages/{pushId}/original')
.onCreate((snap, context) => {
const appOptions = JSON.parse(process.env.FIREBASE_CONFIG);
appOptions.databaseAuthVariableOverride = context.auth;
console.log(dir(context))
const app = admin.initializeApp(appOptions, 'app');
const uppercase = snap.val().toUpperCase();
const ref = snap.ref.parent.child('uppercase');
const deleteApp = () => app.delete().catch(() => null);
return app.database().ref(ref).set(uppercase).then(res => {
// Deleting the app is necessary for preventing concurrency leaks
return deleteApp().then(() => res);
}).catch(err => {
return deleteApp().then(() => Promise.reject(err));
});
});
exports.simpleDbFunction = functions.database.ref('/messages/{pushId}/original')
.onCreate((snap, context) => {
if (context.authType === 'ADMIN') {
// do something
console.log(snap.val(), 'written by', context);
} else if (context.authType === 'USER') {
console.log(snap.val(), 'written by USER', context);
}
每次我尝试从context.auth.uid或context.auth.token.email获取时,它都会返回null,似乎context.auth为null。也许我要跳过一些步骤。
我只想获取当前用户或浏览器会话用户,并更新我在请求新所有者时发送的图书ID。我希望界面尽可能简单,例如扫描条形码或单击。
答案 0 :(得分:0)
如果context.auth的值为空,则意味着在数据库更改的同时,没有用户签名到客户端应用程序。
https://firebase.google.com/docs/functions/database-events#accessing_user_authentication_information
答案 1 :(得分:0)
对于有兴趣的人,如果您已经有一个角度应用程序,则可以通过查询路线中的参数来实现此行为,请检查以下内容: https://alligator.io/angular/query-parameters/