Ember CLI - 在路由中注入服务并使用 ember-simple-auth 获取当前用户
Ember CLI - Injecting a service in route and get the current User with ember-simple-auth
我正在使用 ember-simple-auth 进行身份验证。我已经创建了获取当前用户的服务。
//services/session.js
import Ember from 'ember';
import ESASession from "ember-simple-auth/services/session";
export default ESASession.extend({
store: Ember.inject.service(),
setCurrentUser: function() {
if (this.get('isAuthenticated')) {
let user_id = this.get('session.content.authenticated.user_id');
this.get('store').findRecord('user', user_id).then((user) => {
this.set('currentUser', user);
});
}
}.observes('isAuthenticated')
});
在我注入会话时在控制器中工作
//controller/application.js
import Ember from 'ember';
export default Ember.Controller.extend({
auth: Ember.inject.service('session'),
actions: {
save() {
var current_user = this.get('auth.currentUser');
console.log(current_user) // I got the current user model
}
}
});
但是当我尝试在路线中使用相同的方法时,我的 current_user 未定义
//routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
auth: Ember.inject.service('session'),
model() {
console.log(this.get('auth.currentUser')); // I got undefined
}
});
我需要获取当前用户,因为我想知道用户是否有is_admin标志,比如
afterModel: function(model, transition) {
if (model.is_admin) {
this.transitionTo('admin');
}
}
在路由中注入会话的最佳方法是什么?我做错了什么?
你什么时候验证会话?在我看来,当模型挂钩被触发时,会话没有经过身份验证,这就是你未定义的原因。
我必须更改会话服务
//services/session.js
import Ember from 'ember';
import DS from 'ember-data';
import SessionService from "ember-simple-auth/services/session";
export default SessionService.extend({
store: Ember.inject.service(),
account: Ember.computed('session.content.authenticated.user_id', function() {
const user_id = this.get('session.content.authenticated.user_id');
if (!Ember.isEmpty(user_id)) {
return DS.PromiseObject.create({
promise: this.get('store').findRecord('user', user_id)
});
}
})
});
现在,我可以在路由中使用它了;例如,如果我想在用户具有首次登录标志时重定向
//routes/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(),
sessionAuthenticated() {
var applicationRouteInstance = this;
this.get('session.account').then(function(user){
if (user.get('is_first_login')) {
applicationRouteInstance.transitionTo('users/password-reset');
} else {
applicationRouteInstance.transitionTo('dashboard');
}
});
}
});
我正在使用 ember-simple-auth 进行身份验证。我已经创建了获取当前用户的服务。
//services/session.js
import Ember from 'ember';
import ESASession from "ember-simple-auth/services/session";
export default ESASession.extend({
store: Ember.inject.service(),
setCurrentUser: function() {
if (this.get('isAuthenticated')) {
let user_id = this.get('session.content.authenticated.user_id');
this.get('store').findRecord('user', user_id).then((user) => {
this.set('currentUser', user);
});
}
}.observes('isAuthenticated')
});
在我注入会话时在控制器中工作
//controller/application.js
import Ember from 'ember';
export default Ember.Controller.extend({
auth: Ember.inject.service('session'),
actions: {
save() {
var current_user = this.get('auth.currentUser');
console.log(current_user) // I got the current user model
}
}
});
但是当我尝试在路线中使用相同的方法时,我的 current_user 未定义
//routes/application.js
import Ember from 'ember';
import ApplicationRouteMixin from 'ember-simple-auth/mixins/application-route-mixin';
export default Ember.Route.extend(ApplicationRouteMixin, {
auth: Ember.inject.service('session'),
model() {
console.log(this.get('auth.currentUser')); // I got undefined
}
});
我需要获取当前用户,因为我想知道用户是否有is_admin标志,比如
afterModel: function(model, transition) {
if (model.is_admin) {
this.transitionTo('admin');
}
}
在路由中注入会话的最佳方法是什么?我做错了什么?
你什么时候验证会话?在我看来,当模型挂钩被触发时,会话没有经过身份验证,这就是你未定义的原因。
我必须更改会话服务
//services/session.js
import Ember from 'ember';
import DS from 'ember-data';
import SessionService from "ember-simple-auth/services/session";
export default SessionService.extend({
store: Ember.inject.service(),
account: Ember.computed('session.content.authenticated.user_id', function() {
const user_id = this.get('session.content.authenticated.user_id');
if (!Ember.isEmpty(user_id)) {
return DS.PromiseObject.create({
promise: this.get('store').findRecord('user', user_id)
});
}
})
});
现在,我可以在路由中使用它了;例如,如果我想在用户具有首次登录标志时重定向
//routes/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(),
sessionAuthenticated() {
var applicationRouteInstance = this;
this.get('session.account').then(function(user){
if (user.get('is_first_login')) {
applicationRouteInstance.transitionTo('users/password-reset');
} else {
applicationRouteInstance.transitionTo('dashboard');
}
});
}
});