从 Cloud Functions for Firebase 中的数据库触发器获取用户 ID?
Getting the user id from a database trigger in Cloud Functions for Firebase?
在下面的示例中,有没有办法获取写入 /messages/{pushId}/original 的用户的 uid?
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
console.log('Uppercasing', event.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return event.data.ref.parent.child('uppercase').set(uppercase);
});
更新的答案 (v1.0.0+):
有关详细信息,请参阅 , version 1.0.0
of the Firebase Functions SDK introduced a new context.auth
object which contains the authentication state such as uid
. See "New properties for user auth information"。
原始答案(v1.0.0 之前):
是的,这在技术上是可行的,尽管目前没有记录。 uid
与 event.auth
对象一起存储。当从管理员情况(例如,从 Firebase 控制台数据查看器或从 Admin SDK)触发数据库云功能时,event.auth
的值为:
{
"admin": true
}
当数据库云函数从未经身份验证的引用触发时,event.data
的值为:
{
"admin": false
}
最后,当一个数据库云函数被授权的而非管理员引用触发时,event.auth
的格式是:
{
"admin": false,
"variable": {
"provider": "<PROVIDER>",
"provider_id": "<PROVIDER>",
"user_id": "<UID>",
"token": {
// Decoded auth token claims such as sub, aud, iat, exp, etc.
},
"uid": "<UID>"
}
}
根据以上信息,获取触发事件的用户的 uid
的最佳做法是执行以下操作:
exports.someFunction = functions.database.ref('/some/path')
.onWrite(event => {
var isAdmin = event.auth.admin;
var uid = event.auth.variable ? event.auth.variable.uid : null;
// ...
});
请注意,在上面的代码中,即使 isAdmin
是 true
,uid
也会是 null
。您的确切代码取决于您的用例。
警告: 这是目前未记录的行为,所以我会给出我通常的警告 "undocumented features may be changed at any point in the future without notice and even in non-major releases."
自从 Firebase 函数达到版本 1.0 以来,此行为不再是未记录的,但已略有更改。请务必阅读 the docs.
云函数已添加上下文,您可以这样使用
exports.dbWrite = functions.database.ref('/path/with/{id}').onWrite((data, context) => {
const authVar = context.auth; // Auth information for the user.
const authType = context.authType; // Permissions level for the user.
const pathId = context.params.id; // The ID in the Path.
const eventId = context.eventId; // A unique event ID.
const timestamp = context.timestamp; // The timestamp at which the event happened.
const eventType = context.eventType; // The type of the event that triggered this function.
const resource = context.resource; // The resource which triggered the event.
// ...
});
在下面的示例中,有没有办法获取写入 /messages/{pushId}/original 的用户的 uid?
exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
.onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const original = event.data.val();
console.log('Uppercasing', event.params.pushId, original);
const uppercase = original.toUpperCase();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
// Setting an "uppercase" sibling in the Realtime Database returns a Promise.
return event.data.ref.parent.child('uppercase').set(uppercase);
});
更新的答案 (v1.0.0+):
有关详细信息,请参阅 1.0.0
of the Firebase Functions SDK introduced a new context.auth
object which contains the authentication state such as uid
. See "New properties for user auth information"。
原始答案(v1.0.0 之前):
是的,这在技术上是可行的,尽管目前没有记录。 uid
与 event.auth
对象一起存储。当从管理员情况(例如,从 Firebase 控制台数据查看器或从 Admin SDK)触发数据库云功能时,event.auth
的值为:
{
"admin": true
}
当数据库云函数从未经身份验证的引用触发时,event.data
的值为:
{
"admin": false
}
最后,当一个数据库云函数被授权的而非管理员引用触发时,event.auth
的格式是:
{
"admin": false,
"variable": {
"provider": "<PROVIDER>",
"provider_id": "<PROVIDER>",
"user_id": "<UID>",
"token": {
// Decoded auth token claims such as sub, aud, iat, exp, etc.
},
"uid": "<UID>"
}
}
根据以上信息,获取触发事件的用户的 uid
的最佳做法是执行以下操作:
exports.someFunction = functions.database.ref('/some/path')
.onWrite(event => {
var isAdmin = event.auth.admin;
var uid = event.auth.variable ? event.auth.variable.uid : null;
// ...
});
请注意,在上面的代码中,即使 isAdmin
是 true
,uid
也会是 null
。您的确切代码取决于您的用例。
警告: 这是目前未记录的行为,所以我会给出我通常的警告 "undocumented features may be changed at any point in the future without notice and even in non-major releases."
自从 Firebase 函数达到版本 1.0 以来,此行为不再是未记录的,但已略有更改。请务必阅读 the docs.
云函数已添加上下文,您可以这样使用
exports.dbWrite = functions.database.ref('/path/with/{id}').onWrite((data, context) => {
const authVar = context.auth; // Auth information for the user.
const authType = context.authType; // Permissions level for the user.
const pathId = context.params.id; // The ID in the Path.
const eventId = context.eventId; // A unique event ID.
const timestamp = context.timestamp; // The timestamp at which the event happened.
const eventType = context.eventType; // The type of the event that triggered this function.
const resource = context.resource; // The resource which triggered the event.
// ...
});