Ember Siple Auth current user


#1

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


#2

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}}

#3

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


#4

You can inject into components


#5

Hey @mtangoo , I just updated the article you are referring to: http://emberigniter.com/real-world-authentication-with-ember-simple-auth/

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.


#6

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

Thank you!