Hello, i need to call a method who is define in my application controller since a route,
post/comment/new route because i like to deny access this route for users are not authenticated.
import Ember from 'ember';
import App from '../app';
export default Ember.Controller.extend({
authManager: App.AuthManager,
currentUser: function() {
return App.AuthManager.get('apiKey.user');
}.property('authManager.apiKey'),
isAuthenticated: function() {
return App.AuthManager.isAuthenticated();
}.property('authManager.apiKey'),
});
in my applycation.hbs, i have:
{{#if isAuthenticated}}
and it respond NO,
BUT if a call this method in my post/comment/new.hbs it respon yes, i think it because in the if this method is not declared, so how i do that?
thanks
I assume the code you provided is your application controller? And the App.AuthManager
is some other object/controller you’re using to store the global state for the user. If I am wrong about either of those two assumptions, please let me know.
I think the first thing you should do is make your App.AuthManager
a service. It sounds like functionally it’s probably already close to that. Here is a good reference on doing that: Services - Application Concerns - Ember Guides
Once you’ve done that, your application controller should look something like this:
import Ember from 'ember';
export default Ember.Controller.extend({
authManager: Ember.inject.service(),
currentUser: Ember.computed.reads('authManager.apiKey.user'),
isAuthenticated: Ember.computed.reads('authManager.isAuthenticated')
});
Edit: I wanted to point out that you can also use authManager: Ember.inject.service()
on Components and Routes to access it.