User.emailverified Doesn't Change After Clicking Email Verification Link Firebase
Solution 1:
As mentioned by Tope in a comment, you need to do a firebaseUser.reload()
in order for the change to the firebaseUser's authentication status to be updated.
Solution 2:
I just took the same steps:
auth.currentUser.emailVerified
false
auth.currentUser.sendEmailVerification()
- wait for the email
- click the link in the email
- sign out
- sign in again
And then
auth.currentUser.emailVerified
true
Note step 3 and 4: I needed to sign in again, before the new value of emailVerified
was visible in my app.
This answer also seems relevant, but I only spotted it after writing the above: Firebase confirmation email not being sent
Solution 3:
In a very minimalistic way, this is what I ended up with:
angular.module('theApp')
.controller('emailVerifyController', ['$scope', '$stateParams', 'currentAuth', 'DatabaseRef',
function($scope, $stateParams, currentAuth, DatabaseRef) {
// where currentAuth and DatabaseRef is what they sound like
$scope.doVerify = function() {
firebase.auth()
.applyActionCode($stateParams.oobCode)
.then(function(data) {
// change emailVerified for logged in User
// you can update a DatabaseRef endpoint in here
// whatever!
toastr.success('Verification happened', 'Success!');
})
.catch(function(error) {
$scope.error = error.message;
toastr.error(error.message, error.reason, { timeOut: 0 });
})
};
}
])
Then in template, something like this:
<a ng-click="doVerify()" class="button large">Verify my Account</a>
Although the applyActionCode
is not yet wrapped for AngularFire, you could still drop down to vanilla Javascript of the SDK in your AngularJS stuff, besides, why not!
I share more details for Email Verification in Firebase:
https://blog.khophi.co/email-verification-firebase-3-0-sdk/
Solution 4:
We have just gone through the same issue and found the fix was to both reload user using user.reload()
function and to also change the observer from onAuthStateChanged
to onIdTokenChanged
.
This is because you are updating the firebase token with the new emailVerifed
property and onIdTokenChanged
is listening for changes to the Id token and updates the user object with the correct values.
Code snippets:
export function* register_user(action) {
try {
//Call api which registers user and sets email verified to true
let register_result = yield call(Register_API.create_registration, action.data)
if (register_result && register_result.status >= 200 && register_result.status < 300) {
let user = firebase.auth().currentUser
//Force user to reload so we can trigger the onIdTokenChanged listener
return user.reload()
}
} catch (e) {
console.error(e)
}
}
firebase.auth().onIdTokenChanged(function (user) {
if (user && user.uid) {
if (user.emailVerified) {
//Stuff you want to do
}
}
})
Solution 5:
In my case, it seems currentUser.emailVerified
switches to true
, but not always. Not sure why. It switches to true
with Chrome in most cases, but not always. It does not with Firefox.
Applying the reload()
method seemed to fix the issue.
I have the following in a saga, after loading the URL that contains the oobCode :
const currentUser = firebase.auth().currentUser;
if(currentUser){
currentUser.reload();
}
Post a Comment for "User.emailverified Doesn't Change After Clicking Email Verification Link Firebase"