简体   繁体   English

如何将会话存储在Ember-Simple-Auth会话中?

[英]How can I get the session stored in a Ember-Simple-Auth session?

I'm using ember-simple-auth to manage my app authentication. 我正在使用ember-simple-auth来管理我的应用程序身份验证。

I have implemented my own authenticator, authorizer and adapter. 我已经实现了自己的身份验证器,授权者和适配器。

Source code 源代码

Authenticator 认证者

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

export default Base.extend({

  tokenEndpoint: 'http://localhost:9000/0/auth/',

  restore: function(data) {
    console.log("restore");
    console.log(data);
    return new Ember.RSVP.Promise(function(resolve, reject) {
      if (!Ember.isEmpty(data.token)) {
        resolve(data);
      } else {
        reject();
      }
    });
  },

  authenticate: function(options) {
    return new Ember.RSVP.Promise((resolve, reject) => {
      Ember.$.ajax({
        url: this.tokenEndpoint + options.method,
        type: 'POST',
        data: JSON.stringify(options.data),
        contentType: 'application/json',
        dataType: 'json'
      }).then(function(response) {
        console.log("OK!");
        console.log(response);
        Ember.run(function() {
          console.log("resolve: "+response.data.encodedToken);
          resolve({
            token: response.data.encodedToken
          });
        });

      }, function(xhr, status, error) {
        var response = xhr.responseText;
        console.log("ERROR");
        console.log(response);
        Ember.run(function() {
          reject(response);
        });
      });
    });
  },

  invalidate: function() {
    console.log('invalidate...');
    //return Ember.RSVP.resolve();

    Ember.$.ajax({
        type: 'POST',
        url: this.tokenEndpoint + 'logout',
      }).then(() => {
        resolve(true);
      }, () => {
        reject();
      });
  }
});

Authorizer 授权人

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

export default Base.extend({
  authorize: function(jqXHR, requestOptions) {
          console.log(requestOptions);
          console.log("---- Authorize ----");
          var accessToken = this.get('session.content.secure.token');
          console.log(this.get('session'));
          if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
              jqXHR.setRequestHeader('Authorization', accessToken);
          }
      }
});

Adapter 适配器

import Ember from 'ember';

import JSONAPIAdapter from 'ember-data/adapters/json-api';

import singularize from 'ember-inflector';

export default JSONAPIAdapter.extend({
  namespace: '0',
  host: 'http://localhost:9000',
  session: Ember.inject.service('session'),
  headers: Ember.computed('session.token', function() {
    console.log("Sending header...");
    return {
      'Authorization': 'MYTOKEN'
    };
  }),

  pathForType: function(type) {
    return Ember.String.underscore(type);
    //return singularize(type);
  },

});

Questions 问题

I would like to know how to get the stored token in the authentication process, inside the adapter to inject the encoded token in the header Authorization . 我想知道如何在身份验证过程中获取存储的令牌,在适配器内部将编码的令牌注入标头Authorization

And another question, when is it called the authorizer? 还有一个问题,什么时候称为授权者?

I assume you're using ESA 1.0 or later. 我假设您使用的是ESA 1.0或更高版本。 You don't get the jqXHR in the authorizer's authorize method then but the session data along with a callback instead. 然后,您没有在授权者的authorize方法中获得jqXHR ,而是会话数据和回调。

Check the docs for the DataAdapterMixin and BaseAuthorizer as well as the README for more info. 检查文档中的DataAdapterMixinBaseAuthorizer以及自述文件以获取更多信息。

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

相关问题 Ember-Simple-Auth编码请求标头application / x-www-form-urlencoded,Rails服务器需要application / json - Ember-Simple-Auth encoding request header application/x-www-form-urlencoded, Rails server expects application/json 如何使用json api auth处理angularJS中的会话状态 - How can handle session state in angularJS with json api auth 如何在HTML后面的代码中显示存储在会话变量中的值? - How can I display the value stored in my Session Variable in my code behind, in HTML? 如何解压 jsonlz4 Mozilla Firefox session 文件? - How can I decompress a jsonlz4 Mozilla Firefox session file? Laravel 身份验证会话 cookie 未发送 - Laravel auth session cookie not sending 可以使用jquery会话变量获取? 调用一个PHP - Can get with jquery session variables? calling a php 如何在NodeJS端点中访问Ember-Simple-Auth-Token授权者标头 - How to access Ember-Simple-Auth-Token authorizer header in NodeJS endpoint 如何保存和获取以共享首选项存储的软件包名称? - How can i save and get the package names stored in shared preference? 无法创建会话 - Can't create SESSION 如何将会话或 GAN 模型保存到 js 文件中 - How can I save my session or my GAN model into a js file
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM