Get email address of logged-in user if multiple emails are associated with same account

Often, users associates multiple email addresses with the same account. But question is, is there any way to know using which email user has logged in into the system?

Note : We are strictly using email for log in and not username.

Mongodb users emails array structure:

"emails" : [ 
    {
        "address" : "xyz@abc.com",
        "verified" : true
    },
    {
        "address" : "prq@abc.com",
        "verified" : true
    },
    {
        "address" : "jkl@abc.com",
        "verified" : true
    }
],

Problem statement -
One need to order X item, while putting order in system we need email address of logged in user to save in this particular order. So that one can receive notifications related to this particular order.

If we save email address of logged in user to custom database field while logging in it may work but issue is if another user owning account logs in then second users email will be updated to database and his/her email will get save in order placed by user 1.

Thanks in advance.

Users can login by using username also. Then it’s not able to know what email address is.
Try on login event on the server, it may help. https://docs.meteor.com/api/accounts-multi.html#AccountsCommon-onLogin

of course users can use username but as per project requirement we are logging in using emails only. Right now we are trying workaround to save logged in users email in database field of users collections but issue may occur when same account is accessed by other person at the same time. But I thought meteor may be having this facility to trying my luck. Thank you for your reply I will give try to that also to check what other information it saves as we are already using mizzao:status to save all other information like login status etc.

I suppose it depends on how you are signing users in or what packages you are using, but can’t you just store email in session when signing them in? For instance, just store it in session than pass it to Meteor.loginWithPassword (or whatever you are using to actually auth users). Would that work for you or is there more to your needs?

Thank you for reply but sessions can not survive refresh of the browser.

I haven’t tried, but from my understanding of Meteor, you should be able to store the email used in the connection object, server side. Using Accounts.onLogin(), you can get the email used to authenticate a specific connection, store it in the object representing that connection server side, and take it from there to use when creating new orders.

What about just storing the last logged in email on the users document? ie.

lastLoggedInEmail: "email@example.com"

And then just update this on each login?

Well, they apparently have the use case of multiple users connecting to the same account, using different emails. So if user 2 connects to the shared account while user 1 was placing an order, a “lastLoggedInEmail” would hold wrong value when trying to figure out which user to notify about the order placed by user 1.
Storing the email used to login in the connection instead of the user document would solve that problem, in theory.

Thank you for reply but sessions can not survive refresh of the browser.

Well, you didn’t specify how persistent you need this and already said it cannot be stored in the db as people share accounts. So you need something between session and db then persistence-wise.

I haven’t tried, but from my understanding of Meteor, you should be able to store the email used in the connection object, server side.

I don’t believe this will work either as in my previous use of the conn object, it also gets recreated on refreshes.

If you were to roll your own solution, I would look at a browser-level storage such as localStorage or cookies for a token, and then use this token to tie it back to either an email or a session. Or, you can look at the plethora of packages out there that do this type of thing for you, such as this SO answer:

So, if that works, then you just need to get the email at login time, and store it using some form of storage that ties a browser to an email address. Hope that helps.

1 Like

Ah, interesting, I was wondering about that behavior when thinking about it. I never tried using connection that way before. Good to know !