在Firestore Cloud函数中通过其uid获取身份验证用户 [英] Get a auth user by its uid in Firestore cloud function

本文介绍了在Firestore Cloud函数中通过其uid获取身份验证用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当将新对象添加到集合中时,我会触发云功能.看起来像这样:

I have cloud function triggered when a new object is added to a collection. It looks like this:

exports.emailAdmin = functions.firestore
  .document('user-books/{userId}/books/{ASIN}')
  .onWrite(event => {

event.data.data()是添加到子集合(书")的对象. userId来自Firebase身份验证系统. IE.用户登录,并使用其"uid"将一个对象添加到用户书"集合中.

That event.data.data() is an object added to the sub-collection ("books"). The userId comes from the Firebase authentication system. I.e. a user signed in, and added an object to the collection "user-books" with his/her "uid".

我尝试过:

firestore
    .collection('users')
    .doc(uid)
    .get()

但是,当然"失败了,因为 I 没有名为"users"的集合.如何进入身份验证数据库"?

But that, "of course", fails because I don't have a collection called "users". How do I get to the "authentication database"?

目的是将"uid"转换为该人的电子邮件".

The purpose is to convert the "uid" to that person's "email".

推荐答案

导入firebase-admin:

Import firebase-admin:

import * as admin from 'firebase-admin';

然后获取身份验证用户:

Then fetch the auth user:

const uid = ...;
const authUser = await admin.auth().getUser(uid); // this returns a promise, so use await or .then()

console.log(authUser.email);

这篇关于在Firestore Cloud函数中通过其uid获取身份验证用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆