我可以使用带有快递/护照会话域cookie的ember-simple-auth

mob*_*tta 5 ember.js ember-simple-auth

我有一个项目正在进行,我一直在使用服务器端通过快递/护照域cookie处理我的身份验证和授权,这种cookie通过来回发送sid cookie自动神奇地处理状态.

我没有在客户端上构建很多auth管理,我只是在服务器视图中获取表示将为我引导的用户数据作为全局js对象.我想在ember中更好地处理这个问题,所以我开始实现ember-simple-auth,我能够很好地处理登录,状态等,但看起来它总是依赖于令牌策略.

现在我的代码看起来像这样,你可以看到我必须解决一个带有令牌对象的承诺才能使它工作,但我想要的策略不需要令牌.

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.session.token
                      });
                  });
              }, function(xhr, status, error) {
                  var response = JSON.parse(xhr.responseText);
                  Ember.run(function() {
                      reject(response.error);
                  });
              });
          });
      },
Run Code Online (Sandbox Code Playgroud)

我的问题是,ember-simple-auth可以适应与快递/护照域cookie一起使用,或者我必须更改我的服务器以使用承载策略或oauth2或其他东西.

谢谢.

mar*_*oow 4

Ember Simple Auth 本身不需要令牌。在大多数情况下,“授权者”需要一个令牌才能将其注入到发送至 API 服务器的请求中(请参阅此处: http: //ember-simple-auth.simplabs.com/ember-simple-auth-api-docs .html#SimpleAuth-Authorizers-Base)。如果您使用 cookie,实际上并不需要授权者,因为 cookie 无论如何都会发送到服务器,让它识别经过身份验证的用户。在这种情况下,您可以简单地使用例如方法解析{ authenticated: true }authenticate检查restore方法中的该值:

restore: function(data) {
  return new Ember.RSVP.Promise(function(resolve, reject) {
    if (data.authenticated) {
      resolve(data);
    } else {
      reject();
    }
  });
}
Run Code Online (Sandbox Code Playgroud)

  • @mobetta 教程进行得怎么样?:) (4认同)