[英]Ember-simple-auth cant get currentUser after reload
I have a simple Ember app ( repo ) with authentication using ember-simple-auth (i followed this article https://medium.com/@benhansen/token-based-authentication-with-jwt-ember-and-rails-af54b03fe237 ). 我有一个使用ember-simple-auth进行身份验证的简单Ember应用程序( repo )(我遵循本文https://medium.com/@benhansen/token-based-authentication-with-jwt-ember-and-rails-af54b03fe237 )。 After logging in I have access to currentUser in my routes, controllers and templates.
登录后,我可以在路由,控制器和模板中访问currentUser。 However, if I navigate to any route besides my ApplicationRoute and reload the browser, I can no longer access currentUser in that route, but it's available in the template.
但是,如果我导航到除ApplicationRoute之外的任何路由并重新加载浏览器,则无法再访问该路由中的currentUser,但是模板中可用。 If I navigate back to application route and navigate to different one again, then I can access the currentUser.
如果我导航回应用程序路由并再次导航到其他路由,则可以访问currentUser。 How can I fix this, ie I want to be able to access currentUser in any route even after reload?
如何解决此问题,即即使重新加载后我也希望能够以任何路由访问currentUser?
ApplicationRoute: ApplicationRoute:
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
sessionAccount: Ember.inject.service(),
beforeModel() {
this.get('sessionAccount').loadCurrentUser();
}
});
SessionAccount service: SessionAccount服务:
import Ember from 'ember';
const { inject: { service }, RSVP } = Ember;
export default Ember.Service.extend({
session: service('session'),
store: service(),
loadCurrentUser() {
if (this.get('session.isAuthenticated')) {
this.get('store')
.queryRecord('user', { me: true })
.then((user) => {
this.set('currentUser', user);
});
} else {
return RSVP.resolve();
}
}
});
Bookings route - the 'other' route: 预订路线-“其他”路线:
import Ember from 'ember';
import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';
export default Ember.Route.extend(AuthenticatedRouteMixin, {
sessionAccount: Ember.inject.service(),
model() {
const currentUser = this.get('sessionAccount.currentUser');
return this.get('store').query('booking', {
filter: {
'user_id': currentUser.id
},
include: 'rental,rental.rental-ratings'
});
}
});
Ended just following the tutorial form Ember simple auth github repo. 刚按照教程形式Ember简单auth github存储库结束。 Who would have thought this would work:) Had to change code in app/routes/application.js, also changed the name of my service from 'sessionAccount' to 'currentUser':
谁会想到这会起作用:)不得不更改app / routes / application.js中的代码,也将我的服务名称从“ sessionAccount”更改为“ currentUser”:
// app/routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
const { service } = Ember.inject;
export default Ember.Route.extend(ApplicationRouteMixin, {
currentUser: service(),
beforeModel() {
return this._loadCurrentUser();
},
sessionAuthenticated() {
this._super(...arguments);
this._loadCurrentUser();
},
_loadCurrentUser() {
return this.get('currentUser').load().catch(() => this.get('session').invalidate());
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.