使用模块化JS SDK检查isNewUser与Firebase电子邮件链接验证



这里的官方代码片段是:

// You can access the new user via result.user
// Additional user info profile not available via:
// result.additionalUserInfo.profile == null
// You can check if the user is new or existing:
// result.additionalUserInfo.isNewUser

连API参考都说:

对象,该对象包含作为操作结果的附加用户信息登录、链接或重新认证操作成功。

我得到additionalUserInfo is undefined。我需要检测电子邮件链接登录是否是新用户。

我代码:

await setPersistence(auth, browserLocalPersistence);
const result = await signInWithEmailLink(auth, email.value, window.location.href);
if (result && result.user) {
window.localStorage.removeItem('email');
window.localStorage.removeItem('its');
console.log(result.additionalUserInfo.isNewUser()); // undefined
return router.push({ path: "/dashboard" }); 
}

您需要在Github问题中提到的新的模块化SDK中单独使用getAdditionalUserInfo方法。

import {signInWithEmailLink, getAdditionalUserInfo} from "firebase/auth"
const result = await signInWithEmailLink(auth, email.value, window.location.href);
const {isNewUser} = getAdditionalUserInfo(result)
// Pass the UserCredential                ^^^^^^

最新更新