How to get the currentUser in a controller?


#1

My goal is to get the currently logged in user’s email and save it to the message model when adding a new message to a chat box. What is the best way to get this information into the controller to later be saved in a model? I can currently use session in my .hbs like so {{session.currentUser.email}}


#2

Do you have a user property on your message model? message would belongsTo a user when you createRecord for a new message, the user would be passed along.


#3

If you’re asking how to get at the currentUser in the session (I’m assuming this is a service) in your controller (assuming you have the service injected in the controller somehow already) you can

// If currentUser is a property of the session service
this.get('session.currentUser')

#4

I do indeed have a belongsTo user on the message model but I’m not really sure how to associate a message with the logged in user. I guess that’s why I was trying to use the user session.


#5

I am using torii to start and create sessions like so,

    this.get('session').open('firebase', {
        provider: 'password',
        email: email,
        password: password
     })

Basically I have access to anything session related in my routes. For example,

if(this.get('session.isAuthenticated')){
  this.transitionTo('messages');
}

#6

Have you injected the session into your controller?


#7

I apologize. I actually think that now, the best thing to do would be to figure out how to get the belongsTo, hasMany relationship working between message and user. I’ll take any advice you have regarding that :smiley:


#8

Also I was able to get this to work with

 let sessionName = get(this,'session.currentUser.email');

and then assigning sessionName to the new message’s user


#9

Please also include your user and message models.