Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access user email address in Meteor JS app

I am building an app using Meteor and need to access the stored email address of a logged-in user.

I am currently using:

var userObj = Meteor.user();
console.log(userObj);

to access the user. However, I am only able to access the id. The email address is stored in a nested object that looks like this:

[Object {address="[email protected]", verified=false}]

I have tried various ways to traverse the JSON object but can't figure out how to access the value I need.

like image 591
squeezemylime Avatar asked Dec 22 '12 15:12

squeezemylime


People also ask

How do I send an email using the meteor API?

Documentation of Meteor's email API. The email package allows sending email from a Meteor app. To use it, add the package to your project by running in your terminal: There are two ways on how to setup the package for sending e-mail. First is to set MAIL_URL. The server reads from the MAIL_URL environment variable to determine how to send mail.

What does Meteor’s accounts-password package support?

Let’s look at some common examples that Meteor’s accounts-password package supports out of the box: Password reset. When the user clicks the link in their email, they are taken to a page where they can enter a new password for their account. User enrollment.

What should I add to my Meteor app?

One useful thing to add for your app can be the concept of a “primary” email address. This way, if the user has added multiple emails, you know where to send confirmation emails and similar. Before Meteor 1.2, all email addresses and usernames in the database were considered to be case-sensitive.

How do I register an account in Meteor?

Then save the file, navigate to the applications “Register” page, fill out an email and a password, and click the “Register” button. There won’t be any visual feedback to confirm that an account has been created, but to see that one has, use the find and fetch functions on the “Meteor.users” collection:


3 Answers

Meteor.user().emails[0].address works for me.

Here's what the doc says:

By default the server publishes username, emails, and profile. See Meteor.users for more on the fields used in user documents.

Example user document:

{
  _id: "bbca5d6a-2156-41c4-89da-0329e8c99a4f",  // Meteor.userId()
  username: "cool_kid_13", // unique name
  emails: [
    // each email address can only belong to one user.
    { address: "[email protected]", verified: true },
    { address: "[email protected]", verified: false }
  ],
  createdAt: 1349761684042,
  profile: {
    // The profile is writable by the user by default.
    name: "Joe Schmoe"
  },
  services: {
    facebook: {
      id: "709050", // facebook id
      accessToken: "AAACCgdX7G2...AbV9AZDZD"
    },
    resume: {
      loginTokens: [
        { token: "97e8c205-c7e4-47c9-9bea-8e2ccc0694cd",
          when: 1349761684048 }
      ]
    }
  }
}
like image 85
Dave Avatar answered Oct 19 '22 04:10

Dave


You don't specify how you are authenticating users. For example, if you were using Google authentication only, the email address would be found only in

Meteor.user().services.google.email

So, it depends.

like image 41
David Wihl Avatar answered Oct 19 '22 04:10

David Wihl


Try this:

Meteor.user().emails[0].address

Regards,

like image 3
Ruben Avatar answered Oct 19 '22 04:10

Ruben