Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reloading current user doesn't refresh emailVerified state in Firebase Authentication

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 :(

like image 863
Lovs Avatar asked Aug 19 '18 06:08

Lovs


1 Answers

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.

like image 180
Mica Smith Avatar answered Oct 06 '22 00:10

Mica Smith