Firebase onCreate 将自定义声明添加到 auth.user
Firebase onCreate add custom claims to the auth.user
我正在尝试添加自定义声明,isRegistered to firebase。我的 firestore 还有另一个 user
集合来保存注册信息记录。现在我试图保留一个 isRegistered 自定义声明,但我似乎无法让它工作。
exports.addRegisteredRole = functions.database.ref('/user')
.onCreate((snap, context) => {
return // **I added this later, but the issue still remains.**
admin.auth()
.setCustomUserClaims(context.auth.uid, {isRegistered: true})
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err);
return err
})
});
我正在检查此声明,
currentUser.getIdTokenResult()
.then(res => {
console.log(res.claims.isRegistered)
})
(授权用户对象)。即使我重新登录它仍然未定义。我是不是做错了什么,我是 firebase 的新手。
我怀疑您的 Cloud Function 在对 Auth 的调用完成之前被终止了。这样做的原因是您没有从代码的顶层返回任何内容,这意味着 Cloud Functions 假定您的代码在最后的 }
运行 之后完成。但是由于对 `` 的调用是异步的,该调用尚未完成,例如您的 console.log('done', snap)
还没有 运行。
exports.addRegisteredRole = functions.database.ref('/user')
.onCreate((snap, context) => {
return admin.auth()
.setCustomUserClaims(context.auth.uid, {isRegistered: true})
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err);
return err
})
});
我认为您应该使用 functions.firestore.document 和通配符来触发您的“用户”文档:
exports.addRegisteredRole = functions.firestore.document('/user/{userId}')
.onCreate((snap, context) => { ... });
问题出在您的 onCreate 触发器上。您假设您在 context.auth 对象中获取 uid,这是不正确的。
在您的“用户”集合中添加新文档时,将自动触发 onCreate 触发器。在这种情况下,context.aut.uid 是未定义的。你应该在你的函数日志中跟踪它。
您可以通过多种方式实现您想要做的事情
- 如果用户记录文档以用户Id作为文档名称,可以进行如下操作
exports.addRegisteredRole =
functions.firestore
.document('test/{docId}')
.onCreate((snap, context) => {
admin.auth()
.setCustomUserClaims(snap.id, { isRegistered: true })
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err)
return err
})
})
- 如果您使用其他名称命名用户记录文档,或者让 firebase 为您决定名称,那么您必须将 uid 作为文档数据中的一个属性。然后你使用 docSnapshot.data().uid 而不是 docSnapshot.id 如下
exports.addRegisteredRole =
functions.firestore
.document('test/{docId}')
.onCreate((snap, context) => {
admin.auth()
.setCustomUserClaims(snap.data().uid, { isRegistered: true })
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err)
return err
})
})
祝你好运
问题很可能是您没有将新的自定义声明传播给客户端。看这里:https://firebase.google.com/docs/auth/admin/custom-claims#propagate_custom_claims_to_the_client
这个 github 问题也指出了这一点:https://github.com/firebase/firebase-tools-ui/issues/424
如果您注销并重新登录该用户,它应该可以工作。简单地刷新登录用户的页面不会刷新用户的 ID 令牌。
由于在 onCreate 触发器中设置自定义声明是一个常见的用例,因此最好在专门针对 firestore 的文档 (https://firebase.google.com/docs/auth/admin/custom-claims) 中对此进行注释,因为给定的示例是针对实时数据库。
我正在尝试添加自定义声明,isRegistered to firebase。我的 firestore 还有另一个 user
集合来保存注册信息记录。现在我试图保留一个 isRegistered 自定义声明,但我似乎无法让它工作。
exports.addRegisteredRole = functions.database.ref('/user')
.onCreate((snap, context) => {
return // **I added this later, but the issue still remains.**
admin.auth()
.setCustomUserClaims(context.auth.uid, {isRegistered: true})
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err);
return err
})
});
我正在检查此声明,
currentUser.getIdTokenResult()
.then(res => {
console.log(res.claims.isRegistered)
})
(授权用户对象)。即使我重新登录它仍然未定义。我是不是做错了什么,我是 firebase 的新手。
我怀疑您的 Cloud Function 在对 Auth 的调用完成之前被终止了。这样做的原因是您没有从代码的顶层返回任何内容,这意味着 Cloud Functions 假定您的代码在最后的 }
运行 之后完成。但是由于对 `` 的调用是异步的,该调用尚未完成,例如您的 console.log('done', snap)
还没有 运行。
exports.addRegisteredRole = functions.database.ref('/user')
.onCreate((snap, context) => {
return admin.auth()
.setCustomUserClaims(context.auth.uid, {isRegistered: true})
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err);
return err
})
});
我认为您应该使用 functions.firestore.document 和通配符来触发您的“用户”文档:
exports.addRegisteredRole = functions.firestore.document('/user/{userId}')
.onCreate((snap, context) => { ... });
问题出在您的 onCreate 触发器上。您假设您在 context.auth 对象中获取 uid,这是不正确的。
在您的“用户”集合中添加新文档时,将自动触发 onCreate 触发器。在这种情况下,context.aut.uid 是未定义的。你应该在你的函数日志中跟踪它。
您可以通过多种方式实现您想要做的事情
- 如果用户记录文档以用户Id作为文档名称,可以进行如下操作
exports.addRegisteredRole =
functions.firestore
.document('test/{docId}')
.onCreate((snap, context) => {
admin.auth()
.setCustomUserClaims(snap.id, { isRegistered: true })
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err)
return err
})
})
- 如果您使用其他名称命名用户记录文档,或者让 firebase 为您决定名称,那么您必须将 uid 作为文档数据中的一个属性。然后你使用 docSnapshot.data().uid 而不是 docSnapshot.id 如下
exports.addRegisteredRole =
functions.firestore
.document('test/{docId}')
.onCreate((snap, context) => {
admin.auth()
.setCustomUserClaims(snap.data().uid, { isRegistered: true })
.then(() => {
console.log('done', snap)
return {
message: 'done',
data: snap
}
})
.catch(err => {
console.log('something went wrong', err)
return err
})
})
祝你好运
问题很可能是您没有将新的自定义声明传播给客户端。看这里:https://firebase.google.com/docs/auth/admin/custom-claims#propagate_custom_claims_to_the_client
这个 github 问题也指出了这一点:https://github.com/firebase/firebase-tools-ui/issues/424
如果您注销并重新登录该用户,它应该可以工作。简单地刷新登录用户的页面不会刷新用户的 ID 令牌。
由于在 onCreate 触发器中设置自定义声明是一个常见的用例,因此最好在专门针对 firestore 的文档 (https://firebase.google.com/docs/auth/admin/custom-claims) 中对此进行注释,因为给定的示例是针对实时数据库。