我的应用程序自动匿名签名用户
function onAuthStateChanged(user: FirebaseAuthTypes.User | null) {
console.log(
"Auth State Changed... ",
user?.uid,
user?.phoneNumber,
user?.isAnonymous,
"<<App.js"
);
userCurrentSet(user);
if (!user) auth().signInAnonymously();
if (initializing && user) setInitializing(false);
}
useEffect(() => {
const subscriber = auth().onAuthStateChanged(onAuthStateChanged);
return subscriber; // unsubscribe on unmount
}, []);
要管理匿名帐户,我想在使用其他帐户登录之前删除匿名用户,如下所示
const confirmCode = async () => {
initilizingSet(true);
confirm &&
confirm.verificationId &&
userCurrent &&
(await userCurrent
// -------> Trying to link the anonymous user with PhoneProvider
.linkWithCredential(
auth.PhoneAuthProvider.credential(confirm.verificationId, pinNumber)
)
.catch((e) => {
console.log(e.code, "<<errCode");
// -------> If the linking fails because the user already
// has a different account, I delete the anonymous user
// and login with new account
if (e.code === "auth/credential-already-in-use") {
userCurrent // currentuser Auth Object
.delete() // Auth() delete current user method
.then(() =>
confirm.confirm(pinNumber).catch((e) => {
console.log(e);
initilizingSet(false);
})
)
.catch((e) => {
console.log(e);
initilizingSet(false);
});
} else {
initilizingSet(false);
}
}));
};
我的问题是
subscriber
在我有意登录之前再次以匿名方式登录用户,最终我得到了一个具有不同 id 的新匿名用户!
知道如何解决这个问题吗?
从您的代码来看,您似乎正在将提供商链接到现有的匿名帐户。
在这种情况下,您不应该删除匿名帐户(因为它只会创建一个新帐户,如您所见),而只需链接提供商即可。该过程完成后,您将拥有一个帐户,其 UID 与原始匿名用户相同,但它不再是匿名用户。