【问题标题】:Ember simple auth 1.0.1 custom authenticatorEmber 简单身份验证 1.0.1 自定义身份验证器
【发布时间】:2015-11-30 15:51:42
【问题描述】:

我正在将在 ember-simple-auth: 0.8.0 中完成的现有代码更新为 ember-simple-auth: 1.0.1

有两个问题

  1. 它没有保持会话
  2. REST 调用需要具有 withCredentials: true,不确定我可以在哪里设置它们。

这是我的代码

//config/environment.js
  ENV['ember-simple-auth'] = {
    store: 'simple-auth-session-store:local-storage',
    authorizer: 'authorizer:custom',

    routeAfterAuthentication: '/dashboard',
    routeIfAlreadyAuthenticated: '/dashboard'
  };

我的验证器

//authenticators/custom.js
import Ember from 'ember';
import Base from 'ember-simple-auth/authenticators/base';
import config from '../config/environment';

export default Base.extend({
  restore(data) {
    return new Ember.RSVP.Promise(function (resolve, reject) {
      if (!Ember.isEmpty(data.token)) {
        resolve(data);
      }
      else {
        reject();
      }
    });
  },

  authenticate(options) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({
        type: "POST",
        url: config.serverURL + '/api/users/login',
        data: JSON.stringify({
          username: options.identification,
          password: options.password
        }),
        contentType: 'application/json;charset=utf-8',
        dataType: 'json'
      }).then(function(response) {
        Ember.run(function() {
          resolve(response);
        });
      }, function(xhr) {
        Ember.run(function() {
          reject(xhr.responseJSON || xhr.responseText);
        });
      });
    });
  },

  invalidate(data) {
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({
        type: "POST",
        url: config.serverURL + '/api/users/logout'
      }).then(function(response) {
        Ember.run(function() {
          resolve(response);
        });
      }, function(xhr) {
        Ember.run(function() {
          reject(xhr.responseJSON || xhr.responseText);
        });
      });
    });
  }
});

我的授权人(你可以看到我正在尝试更新我的旧代码)

//authorizers/custom.js
import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';

export default Base.extend({
  authorize(sessionData, block) {
    if (!Ember.isEmpty(sessionData.token)) {
      block('X-CSRF-Token',  sessionData.token);
      block('Content-Type',  'application/json;charset=utf-8');
      block('withCredentials', true);
    }
  }

  //authorize(jqXHR, requestOptions) {
  //  if (!(requestOptions.data instanceof FormData)){
  //    requestOptions.contentType = 'application/json;charset=utf-8';
  //  }
  //
  //  requestOptions.crossDomain = true;
  //  requestOptions.xhrFields = {
  //    withCredentials: true
  //  };
  //
  //
  //  var token = this.get('session.token');
  //  console.error(jqXHR);
  //  if (this.get('session.isAuthenticated') ) {
  //    jqXHR.setRequestHeader('X-CSRF-Token', token);
  //  }
  //}
});

我的应用程序适配器

    import DS from 'ember-data';
    import config from '../../config/environment';
    import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

    export default DS.RESTAdapter.extend(DataAdapterMixin, {
        authorizer: 'authorizer:custom',
        namespace: 'api',
        host: config.serverURL,
    });

仪表板

    import Ember from 'ember';

    import AuthenticatedRouteMixin from 'ember-simple-auth/mixins/authenticated-route-mixin';

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

        needs: 'application',
        setupController: function(controller, model){
            this.controllerFor('application').set('pageTitle', 'Dashboard');
            this._super(controller, model);
        }
    });

如果我这样做 console.log(this.get('session.isAuthenticated'); 它返回 true,但是当我在模板中使用它时它不起作用

    {{#if session.isAuthenticated}}
        1
    {{else}}
        0
    {{/if}}

在我的 laravel 端,我可以看到会话已创建并且用户已登录,在 Ember 端,它之前设置了会话,然后在每个请求中重新发送凭据。现在当它发送另一个请求时。我认为它没有 credentials: True 并且 laravel 返回 401。我还尝试发送垃圾标头,但 laravel CORS 拒绝它不在允许的标头中。

谢谢

【问题讨论】:

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


    【解决方案1】:

    授权人配置设置在 1.0 中不再存在,因为自动授权已被删除。有关如何向传出请求添加授权的信息,请参阅 API 文档:

    此外,您的授权者不应多次调用该块,而应只调用一次,一次传递所有授权数据。

    还要确保将会话服务注入到使用会话的模板的所有控制器和组件中。

    【讨论】:

    • a) 我使用了数据适配器 mixin。 b)我不确定我的代码是否多次调用块。 c)我的应用程序现在有 44 条路线,它将有 2k + 条路线。为什么我必须在每个地方注入会话,而我之前能够做到这一点而无需添加它们
    • block('X-CSRF-Token', sessionData.token);block('Content-Type', 'application/json;charset=utf-8');block('withCredentials', true);。如果您不想将服务注入每个路由,您还可以通过初始化程序中的容器设置自动注入。
    • 嗯,我按照你的建议做了,现在只有一个块调用。我可以使用内容类型和凭据发送 ajax 请求。有2件事。 1) 进行 ajax 调用时,它没有传递凭据。 2)当我重新加载时,我已注销。表示会话未持续。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-04-01
    • 2022-12-10
    相关资源
    最近更新 更多