【问题标题】:How access session username after session is authenticated in other controller会话后如何访问会话用户名在其他控制器中进行身份验证
【发布时间】:2015-11-21 18:58:41
【问题描述】:

我正在使用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 函数.. 我做得对吗?

谢谢。

【问题讨论】:

    标签: javascript ember.js ember-simple-auth


    【解决方案1】:

    使用session 服务。如果您在响应中返回当前用户的详细信息,它们将被持久化在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

    希望对你有帮助...

    【讨论】:

    猜你喜欢
    • 2016-11-22
    • 2011-04-29
    • 2017-12-19
    • 2011-08-03
    • 1970-01-01
    • 2014-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-06-16
    相关资源
    最近更新 更多