英文:
Firebase React Native - User doesn't update automatically
问题
我正在使用Firebase制作电子邮件验证系统。
基本上,我创建了一个页面,用于注册用户,然后登录用户。
我获取用户如下:
import { useAuthState } from "react-firebase-hooks/auth";
const [user, loading, error] = useAuthState(FirebaseAuth)
在下一个页面上,我要求用户检查他的电子邮件,因为我发送了一封确认邮件。
我使用了一个带有[user]作为依赖项的useEffect钩子,在其中使用console.log("test")。
除了在页面加载时,useEffect从未被调用过。
然而,在后台,用户的电子邮件已经被验证,所以应该执行useEffect,因为user.emailVerified
已更改。
我还尝试使用onAuthStateChanged,但是即使在电子邮件被验证后,用户仍然保持为"null"...
const [user, setUser] = useState(null);
useEffect(() => {
const auth = getAuth(firebase_app);
const unsubscribe = onAuthStateChanged(auth, (authUser) => {
if (authUser) {
setUser(authUser);
}
});
return () => unsubscribe();
}, []);
英文:
I'm doing an email verification system using Firebase.
So basically, I made a page which sign up, then sign in a user.
I get the user like so :
import {useAuthState} from "react-firebase-hooks/auth";
const [user, loading, error] = useAuthState(FirebaseAuth)
On the next page I ask the user to check his mails, because I sent a confirmation mail.
I'm using a useEffect hook, with [user] as dependency, where I console.log("test").
The useEffect is never called except at the mount of the page.
And yet, in the background, the email of the user is verified, so useEffect should get executed since user.emailVerified
changed.
I also tried to use onAuthStateChanged, but user was remaining to "null", even after the email got verified.... :
const [user, setUser] = useState(null);
useEffect(() => {
const auth = getAuth(firebase_app);
const unsubscribe = onAuthStateChanged(auth, (authUser) => {
if (authUser) {
setUser(authUser);
}
});
return () => unsubscribe();
}, []);
答案1
得分: 1
这似乎是一个“上下文”问题,你的逻辑仍在使用“旧”的上下文/引用。
以下是我过去如何处理电子邮件确认的方式:
let emailInterval = useRef<NodeJS.Timer>();
useEffect(() => {
emailInterval.current = setInterval(async () => {
// 确保在用户确认电子邮件后,我们拥有最新版本的当前用户
await auth().currentUser?.reload();
// 检查用户是否已确认电子邮件
if (auth().currentUser?.emailVerified) {
handleEmailVerified();
} else {
if (!emailConfirmationHasBeenSent) {
sendEmailConfirmation();
}
}
}, 5000);
return () => clearEmailInterval();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const clearEmailInterval = () => {
if (emailInterval.current) {
clearInterval(emailInterval.current);
}
};
英文:
I'm not 100% sure but I think this a 'context' issue where your logic is still using an 'old' context/reference.
Here's how I've managed the email confirmation in the past:
let emailInterval = useRef<NodeJS.Timer>();
useEffect(() => {
emailInterval.current = setInterval(async () => {
// Make sure we have the most recent version of the current user after
// they've confirmed their email
await auth().currentUser?.reload();
// Check if the user had confirmed their email
if (auth().currentUser?.emailVerified) {
handleEmailVerified();
} else {
if (!emailConfirmationHasBeenSent) {
sendEmailConfirmation();
}
}
}, 5000);
return () => clearEmailInterval();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const clearEmailInterval = () => {
if (emailInterval.current) {
clearInterval(emailInterval.current);
}
};
答案2
得分: 1
Email verification happens out-of-band, as it happens in the users email client or in their browser, and not in your application. This means that your application won't know about the change in verification state, until it refreshes the ID token of the users, which is does:
- When the user signs-in again.
- Automatically once every hour, a few minutes before the current ID token expires.
- When you force it to refresh the ID token by calling
reload()
orgetIdToken(true)
on the user.
This has been covered before, so also have a look at:
- https://stackoverflow.com/questions/73263990/firebase-auth-emailverified-doesnt-updated
- https://stackoverflow.com/questions/66186342/why-firebase-email-verification-doesnt-work(其中包含更多链接)
- https://stackoverflow.com/questions/62980546/email-verification-not-working-on-firebase-email-custom-action-handler
英文:
Email verification happens out-of-band, as it happens in the users email client or in their browser, and not in your application. This means that your application won't know about the change in verification state, until it refreshes the ID token of the users, which is does:
- When the user signs-in again.
- Automatically once every hour, a few minutes before the current ID token expires.
- When you force it to refresh the ID token by calling
reload()
orgetIdToken(true)
on the user.
This has been covered before, so also have a look at:
- https://stackoverflow.com/questions/73263990/firebase-auth-emailverified-doesnt-updated
- https://stackoverflow.com/questions/66186342/why-firebase-email-verification-doesnt-work (which contains many more links
- https://stackoverflow.com/questions/62980546/email-verification-not-working-on-firebase-email-custom-action-handler
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论