Ember Siple Auth current user

I have been searching and trying out this for days and had success with what @emberigniter have written here. The only problem with that approach is that it does not work in routes. The routes seems to load before the session service and causes me a lot of troubles. I have looked unto this, looks good but I cannot get it work.

I need to persist current user so that I can use in my routes. However I cannot find yet a better explanation than the two. I will appreciate any help to solve this.

TIA

hey mtangoo

I don’t fully understand what you’re trying to do with

I need to persist current user so that I can use in my routes

But this is an example how you could get the current user in a route from ESA

// application/route.js or routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';

const { Route, inject, get } = Ember;

export default Route.extend(ApplicationRouteMixin, {
authManager: inject.service('session'),   // get ESA session

// wrap ESA session in model-hook
model() {
  const session = get(this, 'authManager'); 
  // return session or only the current user
  return session;
 }
});

You can display the current user in you template like this:

// application/template.hbs or templates/application.hbs
{{model.currentUser.userName}}

Hi I know of that and I currently use it. I want to inject user as a property in the controller but without using controller. Now I know that is not possible for now

You can inject into components

1 Like

Hey @mtangoo , I just updated the article you are referring to: Real-world Authentication with Ember Simple Auth - Ember Igniter

Now I am using a promise-based computed property that should update the template when it resolves. You should be able to inject the service and use it in your routes, too.

1 Like

It will save me a lot of hacks am currently doing and make my code cleaner

Thank you!

1 Like