简体   繁体   English

EmberJS从ember-simple-auth身份验证器检索当前用户

[英]EmberJS retrieve current user from ember-simple-auth authenticator

Issue: 问题:

I am trying to retrieve the current user logged in from ember-simple-auth by extending ember-simple-auth/authenticators/base . 我正在尝试通过扩展ember-simple-auth/authenticators/base来检索从ember-simple-auth登录的当前用户。 I just want to create a function called currentUser() that will return the username but I get the error whenever I try calling the function: 我只想创建一个名为currentUser()的函数,该函数将返回用户名,但是每当我尝试调用该函数时,都会收到错误消息:

Uncaught TypeError: this.get(...).currentUser is not a function(…)

Attempt: 尝试:

the function currentUser() is defined below: 函数currentUser()定义如下:

// app/authenticators/oauth2.js
import Ember from 'ember';
import Base from 'ember-simple-auth/authenticators/base';

const RSVP = Ember.RSVP;

export default Base.extend({
  restore() {
      // restore user session
  },
  currentUser() {
      return this.get("username");
  }),
  authenticate(username, password) {
    this.set('username', username);
    return new RSVP.Promise((resolve, reject) => {
      Ember.$.ajax({
        url: config.baseURL + "accounts/login",
        data: {
          username: username,
          password: password
        },
        type: 'POST',
        dataType: 'json',
        contentType: 'application/x-www-form-urlencoded',
        complete: (response)=> {
          if (response.responseJSON.isAuthenticated)
            resolve({isAuthenticated: true});
          else
            reject("Wrong credentials.");
        }
      });
    });
  },
  invalidate() {
    // log out
  }
});

I called the function using: 我使用以下函数调用了该函数:

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

export default Ember.Controller.extend({
  session: Ember.inject.service('session'),

  actions: {
    alertSession() {
      console.log("!! " + this.get('session').currentUser());
    },
    invalidateSession() {
      this.get('session').invalidate();
      this.transitionToRoute('login');
    }
  }
});

this.get('session').invalidate(); works, but this.get('session').currentUser(); 可以,但是this.get('session').currentUser(); will return the error mentioned above. 将返回上述错误。 Please let me know how to fix this. 请让我知道如何解决此问题。 Also, I am using Ember 2.5.1. 另外,我正在使用Ember 2.5.1。

Thanks! 谢谢!

In your application controller, you are calling currentUser() from the session service, while you've defined currentUser() in your oauth2.js authenticator. 在您的应用程序控制器中,您正在从会话服务中调用currentUser() ,而您已经在oauth2.js身份验证器中定义了currentUser() Execution could be failing because you have not modified the session service to have a reference to your authenticator's currentUser function. 执行可能会失败,因为您尚未修改会话服务以引用对身份验证器的currentUser函数的引用。

The other function executes successfully because the session service has the reference that is necessary to invoke the designated authenticator's invalidate() function. 另一个函数成功执行,因为会话服务具有调用指定的验证者的invalidate()函数所必需的引用。

You might consider using ember-simple-auth's suggestions for Managing a Current User . 您可以考虑使用ember-simple-auth的建议来管理当前用户 With this, you can explore a solution that utilizes an isolated currentUser service that works in conjunction with the session service. 这样,您可以探索一种解决方案,该解决方案利用与会话服务结合使用的隔离currentUser服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM