我从session.isAuthenticated
获得了不同的行为,具体取决于我如何访问它。这可能只是我真正问题的症状。
会发生什么:
session.isAuthenticated
不会更改显示状态(即继续
表现得好像是假的或未定义的)this.get('session.isAuthenticated')
我得到true
当这只是torii时,一切正常,所以当我以某种方式添加ember-simple-auth时我已经搞砸了。
我收到了几个"DEPRECATION: Using the injected 'container' is deprecated. Please use the 'getOwner' helper instead to access the owner of this object
警告,但是当它只是torii时,这些警告工作正常,所以我认为它们仍然没有问题。
模板/ application.hbs
<header>
<nav class="nav-main">
{{#if session.isAuthenticated}}
<button {{action 'invalidateSession'}}>Sign out</button>
{{else}}
<button {{action 'authenticate'}}>Sign in</button>
{{/if}}
Auth: {{session.isAuthenticated}}<br>
<button {{action 'icanhaz'}}>test?</button>
</nav>
</header>
路由/ application.js中
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
session: Ember.inject.service('session'),
actions: {
authenticate() {
this.get('session').authenticate('authenticator:torii', 'myprovider');
},
invalidateSession() {
this.get('session').invalidate();
},
icanhaz() {
console.log(this.get('session.isAuthenticated'));
}
}
});
适配器/ application.js中
import DS from 'ember-data';
export default DS.JSONAPIAdapter.extend({
host: 'https://myprovider.herokuapp.com',
namespace: 'api/v2'
});
认证器/ torii.js
import Ember from 'ember';
import ToriiAuthenticator from 'ember-simple-auth/authenticators/torii';
export default ToriiAuthenticator.extend({
torii: Ember.inject.service(),
})
environment.js
module.exports = function(environment) {
var ENV = {
modulePrefix: 'my-app-prefix',
environment: environment,
baseURL: '/',
locationType: 'auto',
},
contentSecurityPolicy: {
'connect-src': "'self' myprovider.herokuapp.com",
},
torii: {
providers: {
'myprovider': {
apiKey: '6T7hlTUqfYMgcxkXPAOeNzVmC5L26bTYe9A8D5fc',
scope: 'read write',
redirectUri: 'http://localhost:4200'
}
}
}
};
};
答案 0 :(得分:1)
您必须将会话服务注入所有支持您要使用会话的模板的控制器/组件。当您在application
模板中使用会话时,您必须注入会话服务在application
控制器中。在application
路径中注入它不会使其在模板中可用。