Flutter: How to listen to the FirebaseUser is Email verified boolean?

后端 未结 8 681
走了就别回头了
走了就别回头了 2021-01-01 20:38

My Idea: I want to use the Firebase Auth Plugin in Flutter to register the users. But before they can access the App, they have to verify their Email addres

相关标签:
8条回答
  • 2021-01-01 21:32

    This verification isn't as straightforward as you'd hope. First, there is the problem of recognizing that the user has verified their email. Second, there is the issue that there isn't any sort of a notification you can listen to that will automatically trigger a change in your app.

    Check this thread for info about emailVerified: https://github.com/flutter/flutter/issues/20390#issuecomment-514411392

    I was only able to verify the user if I 1) Created their account, 2) Signed them in, 3) Then checked to make sure they verified their email.

    final FirebaseAuth _auth = FirebaseAuth.instance;
    
    var _authenticatedUser = await _auth.signInWithEmailAndPassword(email: _email, password: _password); 
    
    //where _email and _password were simply what the user typed in the textfields.
    
    
    
    if (_authenticatedUser.isEmailVerified) {
            //Verified
          } else {
            //Not verified
            }
    

    Part 2: How do you get your app to recognize that the user has confirmed their email? Find a way to trigger the function that checks confirmation. A button would be easy enough. If you want it to see "automatic" then I guess you could create a timer that checks for email verification every 10 seconds or so.

    0 讨论(0)
  • 2021-01-01 21:33

    I just faced the same situation in my app. My solution was to create a periodic timer into the initState method of a strategic route to hold the app until the e-mail is verified. It is not so elegant as using a listener but works fine.

    bool _isUserEmailVerified;
    Timer _timer;
    
    @override
    void initState() {
        super.initState();
        // ... any code here ...
        Future(() async {
            _timer = Timer.periodic(Duration(seconds: 5), (timer) async {
                await FirebaseAuth.instance.currentUser()..reload();
                var user = await FirebaseAuth.instance.currentUser();
                if (user.isEmailVerified) {
                    setState((){
                        _isUserEmailVerified = user.isEmailVerified;
                    });
                    timer.cancel();
                }
            });
        });
    }
    
    @override
    void dispose() {
        super.dispose();
        if (_timer != null) {
            _timer.cancel();
        }
    }
    
    0 讨论(0)
提交回复
热议问题