最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Reloading current user doesn't refresh emailVerified state in Firebase Authentication - Stack Overflow

programmeradmin4浏览0评论

I've been trying to fix the problem for a whole day and couldn't make it work. I'm getting irritated, firebase docs are such a mess it's insane..

So I'm trying to implement email verification on my React app. I wen't with the docs, and google do send the email, I can click it, it's all good. But, the email verified state doesn't change at all, and believe me I've went through all the stackoverflow topics.

firebase.auth().doSignInWithEmailAndPassword(email, password)
        .then(() => {
            console.log(firebase.auth().currentUser);
            firebase.auth().currentUser.reload()
                .then(() => {
                    console.log(firebase.auth().currentUser);
                })
        })

So I've found i need to reload the user to see the changes applied, tho they just won't work no matter what I do. Both console logs return email.verified = false at all times. I'm hopeless, anyone have any idea on how to make this work? I was wondering whether me setting up custom domain as a verification link has to do anything with that? I'm testing on localhost, link is linking to live website. Please help :(

I've been trying to fix the problem for a whole day and couldn't make it work. I'm getting irritated, firebase docs are such a mess it's insane..

So I'm trying to implement email verification on my React app. I wen't with the docs, and google do send the email, I can click it, it's all good. But, the email verified state doesn't change at all, and believe me I've went through all the stackoverflow topics.

firebase.auth().doSignInWithEmailAndPassword(email, password)
        .then(() => {
            console.log(firebase.auth().currentUser);
            firebase.auth().currentUser.reload()
                .then(() => {
                    console.log(firebase.auth().currentUser);
                })
        })

So I've found i need to reload the user to see the changes applied, tho they just won't work no matter what I do. Both console logs return email.verified = false at all times. I'm hopeless, anyone have any idea on how to make this work? I was wondering whether me setting up custom domain as a verification link has to do anything with that? I'm testing on localhost, link is linking to live website. Please help :(

Share Improve this question edited Aug 19, 2018 at 6:34 Frank van Puffelen 599k85 gold badges889 silver badges859 bronze badges asked Aug 19, 2018 at 6:28 LovsLovs 4014 silver badges7 bronze badges 3
  • 1 Are you sending the verification link, clicking it and processing (calling applyActionCode) it in your custom landing page before you reload() the currentUser? – bojeil Commented Aug 20, 2018 at 2:25
  • I'm not calling applyActionCode anywhere. Are you saying I need to extract the code from URL and call that function? – Lovs Commented Aug 20, 2018 at 4:09
  • 1 Yes, if you are building your custom landing page and not using the default one. Learn more about this from the official docs: firebase.google./docs/auth/custom-email-handler – bojeil Commented Aug 21, 2018 at 7:03
Add a ment  | 

2 Answers 2

Reset to default 7

The method you should call is signInWithEmailAndPassword, so remove the "do" in the function call:

firebase.auth().signInWithEmailAndPassword(email, password)
        .then((credential) => {
            const currentUser = credential.user;
            console.log(firebase.auth().currentUser);
            firebase.auth().currentUser.reload()
                .then(() => {
                    console.log(firebase.auth().currentUser);
                })
        }).catch((err) => {console.error("Problem with sign in ", err);}

Use the credential returned from this function and assign its user value to currentUser to validate that the user has been authenticated.

Lastly, remember to add a catch at the end whenever you use then. In this case, err will contain a property err.code that will tell you why the user could not be authenticated.

I had the same problem. You need to reload your current user using a timer and then listen to user changes (not auth changes) in a Streambuilder. As soon as the user clicks the link they will be redirected to your main page.

Use a Streambuilder to listen to changes to the user:

  StreamBuilder<User?>(
     stream: FirebaseAuth.instance
        .userChanges(), // monitor the changes to the user
     builder: (ctx, snapshot) {

      if (snapshot.connectionState == ConnectionState.waiting)
        return LoadingScreen(); // show loading screen while loading
     
     if (snapshot.hasData && !snapshot.data!.emailVerified) {
        return VerifyEmailScreen();
      }  // if not verified show the verify email screen
      
     
      if (snapshot.hasData) {
        return MainScreen();
      } // if logged in already show main screen

      return OnboardingScreen(); // if not logged in already show the auth screen
    },
  ),

In your VerifyEmailScreen() create a timer in it's initState like this:

  void initState() {

    Timer.periodic(Duration(seconds: 5), (timer) {
      FirebaseAuth.instance.currentUser?.reload();
    });

   super.initState();
  }

I hope it helps your problem. Cheers

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论