会话后访问会话用户名如何在其他控制器中进行身份验证

How access session username after session is authenticated in other controller

我正在使用 Ember-simple-auth 来验证我的路线,并且工作正常。我有:

login - login route
index - after login in ok

我正在使用 ember-cli-notification 弹出此消息:

"Welcome USER NAME"

像这样,在IndexController中:

export default Ember.Controller.extend({
  init () {
    this.notifications.addNotification({
      message: 'Welcome USER NAME',
      type: 'success',
      autoClear: true,
      clearDuration: 5000
    });
  }
});

我想问...如何访问当前登录用户的属性?我在一个完全不同的控制器中,所以我不知道该怎么做...

而且..我想问一下是否可能..我为此使用了 init 函数..我做对了吗?

谢谢。

使用session服务。如果您在响应中 return 当前用户的详细信息,它们将保留在 session.data.authenticated.

// app/controllers/index.js
import Ember from 'ember';

export default Ember.Controller.extend({
 session: Ember.inject.service(),
 init () {
   this.notifications.addNotification({
     message: `Welcome ${this.get('session.data.authenticated.name')}`,
     type: 'success',
     autoClear: true,
     clearDuration: 5000
   });
 }
});

参考Docs& Dummy app

希望对您有所帮助...