【问题标题】:Ember simple-auth mixins deprected不推荐使用 Ember 简单身份验证混合
【发布时间】:2015-06-19 00:15:06
【问题描述】:

我是一位经验丰富(55 年以上)的程序员,但在 ember 和 js 方面完全是菜鸟。我正在尝试使用 ember-cli 插件 ember-cli-simple-auth、ember-cli-simple-auth-oauth2 和 simplelabs 教程中的剪切和粘贴来获得一个简单的身份验证页面。

我在控制台中得到以下信息:

弃用:不推荐使用 LoginControllerMixin。直接使用会话的身份验证方法。

和:

弃用:AuthenticationControllerMixin 已弃用。直接使用会话的身份验证方法。

解决方案可能是微不足道的,但我一直在追逐它几个小时,并在到达死胡同之前深入研究了 javascript。导致这些错误的代码是:

从'simple-auth/mixins/login-controller-mixin'导入LoginControllerMixin;

导出默认 Ember.Controller.extend(LoginControllerMixin, { 身份验证器:'simple-auth-authenticator:oauth2-password-grant' });

它在 Bower 代码中的某处调用 ApplicationControllerMixin。

在我通过将一些旧的 html/ruby/pascal 代码翻译成 js 来“重新发明轮子”之前,谁能帮我“直接使用会话的身份验证方法。”?

谢谢。

【问题讨论】:

    标签: authentication ember.js


    【解决方案1】:

    我觉得你很痛苦。我花了几个星期试图解决这个问题。问题的很大一部分是在过去的几年中发生了很大的变化,并且有很多代码示例已经过时或无法协同工作。很难将各个部分连贯地放在一起,并弄清楚不需要做什么。

    也就是说,请记住我也是 n00b。我所做的似乎工作正常,但我不知道是否有更好的方法。

    另外,您尝试做的可能与我所做的不同。我的应用程序使用Simple-Auth-Torii 对 google(以及 twitter、fb 等)进行身份验证,然后将返回的身份验证代码交换为身份验证令牌。最后一部分发生在服务器上。因此,在会话通过身份验证后,我将身份验证代码传递给服务器并取回身份验证代码。

    // routes/login.js
    import Ember from "ember";
    import ENV from "../config/environment";
    
    export default Ember.Route.extend({
      setupController: function(controller, model) {
        controller.set("errorMessage", null);
      },
    
      actions: {
        googleLogin: function() {
          var _this = this;
    
          // Using "session's authenticate method directly" right here
    
          this.get("session").authenticate("simple-auth-authenticator:torii", "google-oauth2")
            .then(function() {
              // We're now authenticated. The session should now contain authorization
              // code from provider. We now need to exchange that for an auth token.
              var secureData = _this.get("session.content.secure");
    
              // call server to initiate token exchange from provider
              var exchangeData = {
                authorizationCode: secureData.authorizationCode,
                redirectUri    : secureData.redirectUri,
                provider     : 'google'
              };
    
              // sends ajax request to server, which will in turn call provider
              // with authentication code and receive auth token
              _this.tokenService.fetch(exchangeData).then(function(response) {
    
                if (response.success) {
                  _this.set("session.content.secure.access_token", response.data.token);
                  _this.set("session.content.secure.userData", response.data.user);
    
                  // take user somewhere ...
                  _this.transitionTo("data_sets");
                }
                else {
                  // set an error message, log the response to console, whatever, but
                  // we need to invalidate session because as far as simple-auth
                  // is concerned we're already authenticated. The following logs the user out.
                  _this.get("session").invalidate();
                }
              }, function(error) {
                console.log("tokenService.fetch error", error);
                _this.get("session").invalidate();
              });
    
            }, function(error) {
              console.log("simple-auth-authenticator:torii error", error);
              _this.get("session").invalidate();
            });
        },
    
        twitterLogin: function() {
          // etc.
        }
      }
    });
    

    注销用户也直接使用会话。

    {{!templates/application.hbs}}
    <ul class="nav navbar-nav navbar-right">
    {{#if session.isAuthenticated}}
      <li><button {{ action 'invalidateSession' }} class="btn btn-sm">Logout</button></li>
    {{/if}}
      ...
    </ul>
    
    
    // routes/application.js
    import Ember from "ember";
    import ENV from "../config/environment";
    import ApplicationRouteMixin from "simple-auth/mixins/application-route-mixin";
    
    export default Ember.Route.extend(ApplicationRouteMixin, {
    
      actions: {
        // action is globally available because in application route
        invalidateSession: function() {
          // the most basic logout
          this.get("session").invalidate();
          return;
    
          // If you need to invalidate also on the server do something like:
          //
          // var _this = this;
          // return new Ember.RSVP.Promise(function(resolve, reject) {
          //   var params = {
          //     url    : ENV.logoutEndpoint,
          //     type     : "POST",
          //     dataType   : "json"
          //   };
    
          //   Ember.$.ajax(params).then(function(response) {
          //     console.log('session invalidated!');
          //     console.dir(response);
          //     _this.get("session").invalidate();
          //   });
          // });
        }
      }
    });
    

    【讨论】:

    • 令牌交换实际上应该包含在自定义身份验证器中,请参见例如此博文供参考:engineering-blog.alphasights.com/…。在身份验证器而不是路由操作中执行所有这些操作可以显着简化代码。
    【解决方案2】:

    我遇到了同样的弃用问题。我认为这个用于可爱的登录控制器的 sn-p 就可以了,你问的有点多,但我希望它仍然可以理解。我用它来设计,除了我这样用它几乎一样:authenticate('simple-auth-authenticator:devise', credentials)

    import Ember from 'ember';
    
    export default Ember.Controller.extend({
      actions: {
        authenticate: function() {
          // identification and password are the names of the input fields in the template
          var credentials = this.getProperties('identification', 'password');
    
          if (!credentials.identification || !credentials.password) {
            return false;
          }
    
          this.get('session').authenticate('simple-auth-authenticator:oauth2-password-grant', credentials).then(function() {
            // authentication was successful
          }, function(errorMessage) {
            // authentication failed
          });
        }
      }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-08-28
      • 1970-01-01
      • 2014-06-24
      • 2011-03-12
      相关资源
      最近更新 更多