【问题标题】:Invalidate session with custom authenticator使用自定义身份验证器使会话无效
【发布时间】:2014-11-03 17:32:44
【问题描述】:

使用 ember-cli 0.1.2 和 ember-cli-simple-auth 0.7.0,我需要使客户端和服务器上的会话无效。正如here 解释的那样,我需要做一些类似于authenticate 方法的事情,向服务器发出ajax 请求并确保在清空会话之前成功:

import Ember from 'ember';
import Base from "simple-auth/authenticators/base";

var CustomAuthenticator = Base.extend({
  tokenEndpoint: 'http://127.0.0.1:3000/api/v1/auth/login',

  restore: function(data) {

  },

  authenticate: function(credentials) {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({
        url:         _this.tokenEndpoint,
        type:        'POST',
        data:        JSON.stringify({ email: credentials.identification, password: credentials.password }),
        contentType: 'application/json'
      }).then(function(response) {
        Ember.run(function() {
          resolve({ token: response.token });
        });
      }, function(xhr, status, error) {
        var response = JSON.parse(xhr.responseText);
        Ember.run(function() {
          reject(response.error);
        });
      });
    });
  },

  invalidate: function() {
    var _this = this;
    return new Ember.RSVP.Promise(function(resolve, reject) {
      Ember.$.ajax({ 
        url: _this.tokenEndpoint, 
        type: 'DELETE' 
      }).then(function(response) {
        resolve();
      }, function(xhr, status, error) {
        var response = JSON.parse(xhr.responseText);
        Ember.run(function() {
          reject(response.error);
        });
      });
    });
  }

  // invalidate: function() {
  //   var _this = this;
  //   return new Ember.RSVP.Promise(function(resolve) {
  //     Ember.$.ajax({ url: _this.tokenEndpoint, type: 'DELETE' }).always(function() {
  //       resolve();
  //     });
  //   });
  // }
});

export default {
  name : 'authentication',
  before : 'simple-auth',
  initialize : function(container) {
    container.register('authenticator:custom', CustomAuthenticator);
  }
};

我的注销 API 端点需要令牌(在标头中)。我如何通过它?我读了this,但我的授权人似乎忽略了它,我得到了 401:

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

var CustomAuthorizer = Base.extend({
  authorize: function(jqXHR, requestOptions){
    Ember.debug("AUTHORIZING!");
  }
});

export default {
  name : 'authorization',
  before : 'simple-auth',
  initialize : function(container) {
    container.register('authorizer:custom', CustomAuthorizer);
  }
};

我的environment.js

/* jshint node: true */

module.exports = function(environment) {
  var ENV = {
    modulePrefix: 'wishhhh',
    environment: environment,
    baseURL: '/',
    locationType: 'auto',
    EmberENV: {
      FEATURES: {
        // Here you can enable experimental features on an ember canary build
        // e.g. 'with-controller': true
      }
    },

    APP: {
      // Here you can pass flags/options to your application instance
      // when it is created
    }
  };

  // TODO: disabled because of https://github.com/stefanpenner/ember-cli/issues/2174
  ENV.contentSecurityPolicyHeader = 'Disabled-Content-Security-Policy'

  ENV['simple-auth'] = {
    authorizer: 'authorizer:custom',
    // crossOriginWhitelist: ['http://localhost:3000']
    crossOriginWhitelist: ['*']
  }

  if (environment === 'development') {
    // ENV.APP.LOG_RESOLVER = true;
    ENV.APP.LOG_ACTIVE_GENERATION = true;
    // ENV.APP.LOG_TRANSITIONS = true;
    // ENV.APP.LOG_TRANSITIONS_INTERNAL = true;
    ENV.APP.LOG_VIEW_LOOKUPS = true;
  }

  if (environment === 'test') {
    // Testem prefers this...
    ENV.baseURL = '/';
    ENV.locationType = 'auto';

    // keep test console output quieter
    ENV.APP.LOG_ACTIVE_GENERATION = false;
    ENV.APP.LOG_VIEW_LOOKUPS = false;

    ENV.APP.rootElement = '#ember-testing';
  }

  if (environment === 'production') {

  }

  return ENV;
};

当我最终尝试注销时,以下是 Ember 检查器的输出:

【问题讨论】:

  • BTW:你需要安装authenticator的restore方法

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


【解决方案1】:

您是否真的将 Ember Simple Auth 配置为使用您的自定义授权方?在这种情况下,它应该自动授权会话失效请求。

或者,您可以将令牌添加到验证器的无效方法中,该方法会传递会话的内容。

【讨论】:

  • 嗨,马可。我用environment.js 编辑了我的问题。可能问题与this 有关。但我无法尝试,因为我不知道我应该把那个 sn-p 放在哪里
  • 您不再需要window.ENVEmber.debug("AUTHORIZING!"); 是否实际执行过(当然,您当然需要在该方法中实际添加标头)?
  • 不,它没有被执行。我从 Ember Inspector 注意到存在一个名为 custom 的实际授权容器,并且在其属性中 authenticator: authenticator:custom 可能是错误的。请查看随附的屏幕截图...
  • 您看到的是authorizer:custom,它的季节属性具有authenticator 属性。授权人是否授权了其他请求?
  • 通过尝试解决...这是与 CORS 相关的问题。我不得不将crossOriginWhitelist: ['http://127.0.0.1:3000'] 用于ENV['simple-auth']。以前对['*']['http://localhost:3000'] 的尝试没有奏效。非常感谢马可甚至引导我走上正轨
【解决方案2】:

感谢marcoow,我发现实际上每个请求都存在问题,而不仅仅是注销请求。我的授权人从未接到电话。问题是crossOriginWhitelist 的环境设置,为了使用我的开发API,我必须设置为['http://127.0.0.1:3000']['http://localhost:3000'][*] 都不起作用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 2017-12-19
    相关资源
    最近更新 更多