我的问题如下:我有一个系统,其中包含一些资产信息(例如,一本书),并且该资产具有一些特征(例如,标题、当前所有者、颜色)。我希望能够通过执行一个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。我希望界面尽可能简单,就像扫描条形码或一键点击。
发布于 2019-09-04 03:23:23
如果context.auth的值为null,这意味着在数据库更改的同时没有用户登录到客户端应用程序。
https://firebase.google.com/docs/functions/database-events#accessing_user_authentication_information
发布于 2019-09-05 20:38:27
对于感兴趣的应用程序,如果您已经有一个angular应用程序,则可以通过查询路由中的参数来实现此行为,请检查以下内容:https://alligator.io/angular/query-parameters/
https://stackoverflow.com/questions/57777343
复制相似问题