React Native应用:如何通过邮箱获取Firebase Auth的UID以实现好友添加
通过Firebase Auth + Firestore实现React Native中邮箱转UID的方案
嘿,这个需求我之前做好友系统的时候刚好碰到过!Firebase Auth本身并没有提供直接通过邮箱地址获取用户UID的公开API——这是出于用户隐私和安全的考量,防止第三方随意获取用户的身份关联信息。不过我们可以借助Firestore来间接实现这个功能,下面是具体的实现步骤和代码示例:
1. 提前存储邮箱与UID的映射关系
在用户注册(或者首次登录)的时候,我们把用户的邮箱(统一转成小写,避免大小写问题)和对应的UID存入Firestore的一个专用集合里。这样后续就能通过邮箱快速查询到UID了。
import { getAuth, createUserWithEmailAndPassword, signInWithEmailAndPassword } from "firebase/auth"; import { getFirestore, doc, setDoc, getDoc } from "firebase/firestore"; const auth = getAuth(); const db = getFirestore(); // 注册时同步存储映射 const handleSignUp = async (email, password) => { try { const userCredential = await createUserWithEmailAndPassword(auth, email, password); const user = userCredential.user; // 用小写邮箱作为文档ID,方便后续精准查询 const normalizedEmail = user.email.toLowerCase(); await setDoc(doc(db, "emailToUidMap", normalizedEmail), { uid: user.uid }); // 注册成功后的逻辑... } catch (error) { console.error("注册出错:", error); } }; // 首次登录时补存映射(兼容之前注册的老用户) const handleSignIn = async (email, password) => { try { const userCredential = await signInWithEmailAndPassword(auth, email, password); const user = userCredential.user; const normalizedEmail = user.email.toLowerCase(); const docRef = doc(db, "emailToUidMap", normalizedEmail); const docSnap = await getDoc(docRef); if (!docSnap.exists()) { await setDoc(docRef, { uid: user.uid }); } // 登录成功后的逻辑... } catch (error) { console.error("登录出错:", error); } };
2. 通过邮箱查询对应的UID
当用户输入待添加好友的邮箱后,我们就可以去Firestore的emailToUidMap集合里查询对应的UID了。
import { getFirestore, doc, getDoc, updateDoc, arrayUnion } from "firebase/firestore"; import { getAuth } from "firebase/auth"; const db = getFirestore(); const auth = getAuth(); // 根据邮箱获取UID的工具函数 const getUidFromEmail = async (targetEmail) => { try { // 统一转小写,和存储时保持一致 const normalizedEmail = targetEmail.toLowerCase(); const docRef = doc(db, "emailToUidMap", normalizedEmail); const docSnap = await getDoc(docRef); if (docSnap.exists()) { return docSnap.data().uid; } else { // 没有找到对应注册用户 return null; } } catch (error) { console.error("查询UID失败:", error); return null; } }; // 好友添加逻辑 const handleAddFriend = async (friendEmail) => { const currentUser = auth.currentUser; if (!currentUser) { alert("请先登录"); return; } const friendUid = await getUidFromEmail(friendEmail); if (!friendUid) { alert("该邮箱未注册账号"); return; } if (friendUid === currentUser.uid) { alert("不能添加自己为好友"); return; } // 将好友UID关联到当前用户的好友列表(用arrayUnion避免重复添加) await updateDoc(doc(db, "userFriends", currentUser.uid), { friendUids: arrayUnion(friendUid) }); alert("好友添加成功!"); };
3. 配置Firestore安全规则
为了保护用户隐私,一定要给相关集合设置合理的安全规则,防止恶意查询或修改:
rules_version = '2'; service cloud.firestore { match /databases/{database}/documents { // 邮箱转UID的映射集合 match /emailToUidMap/{email} { // 只允许已登录用户查询 allow read: if request.auth != null; // 只允许用户自己写入/更新自己的邮箱映射 allow write: if request.auth != null && request.resource.data.uid == request.auth.uid; } // 用户好友列表集合 match /userFriends/{uid} { // 只允许用户自己读写自己的好友列表 allow read, write: if request.auth != null && request.auth.uid == uid; } } }
一些注意事项
- 邮箱大小写统一:Firebase Auth对邮箱的大小写是不敏感的,但Firestore的文档ID是大小写敏感的,所以存储和查询时一定要转成统一的小写(或大写)。
- 更新映射:如果用户后续修改了邮箱,记得同步更新
emailToUidMap集合里的文档(删除旧邮箱的文档,添加新邮箱的文档)。 - 隐私保护:不要在这个映射集合里存储其他敏感信息,只保留邮箱和UID的对应关系即可。
内容的提问来源于stack exchange,提问作者Sazzad Hossain
相关产品推荐
相关产品推荐

