Check if user email is verified

I’m trying to render menu dynamically depending on if user’s email is verified.
It works in console when I type Meteor.user().email[0].verified() which returns true/false but in my react component when I use it, it says cannot read property ‘emails’ of undefined. What do I do?

Do you have some kind of check for wether your subscriptions are ready?

IIRC, internally Meteor.user() is function which subscribes to your current user and thus returns the user object. When your component is loaded first, your subscriptions may not be ready yet, resulting in Meteor.user() being undefined the first time your component is rendered.

No, I didn’t use any. How would I do that?

This is what I did…
if(Meteor.user().email[0].verified()) {...}

Also this maybe a separate question but how do I set state on react component based on Meteor.userId() ? To see if user is logged in or not?
I did this.state = { isLoggedIn: Meteor.userId() } but it doesn’t change state when I try log in/out. Only when page refreshes and it reloads everything it changes state. Can I get some help on that as well?

I used TrackerReact to subscribe to custom Mongo Collections to update states in React but I don’t know how to do that with users…

Well, in this case, the easiest thing to do, is to make a check

if (Meteor.user() && Meteor.user().email[0].verified()) {

And your else should return some kind of loading component, indicating to the user that the data is still being loaded.

else {
  return <LoadingBar />
}

Where LoadingBar is a component you will create yourself. Trust me, you’ll be using it in a lot of places. You COULD also return null but that’s not such a great user experience.

Here you have a function for Router for example (it works well):

function emailVerified (user) {
  return _.some(user.emails, function (email) {
    return email.verified
  })
}

This check if someone email u have asociated in your account is verified.

1 Like