dimanche 2 juillet 2017

Set value session authorizer ember simple-auth

I use ember simple-auth with devise_token in rails, in my adapter my function handleResponse is not invoqued, Why?

My authentication works but authorized not work and my session i can't get the atributes is null


  <br>
  HI
   


this is my adapter code

    // `adapters/application.js`

import Ember from 'ember';
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

const { service } = Ember.inject;
import config from '../config/environment';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
  authorizer: 'authorizer:authorizer-devise',
  host: `${config.apiHost}`,
  namespace: `${config.apiNamespace}`,
  session: service('session'),

  handleResponse(status, headers) {

    if(headers['client']) {
      let newSession = this.get('session.data');
      newSession['authenticated']['accessToken'][0] = headers['access-token'];
      newSession['authenticated']['expiry'][0] = headers['expiry'];
      newSession['authenticated']['tokenType'][0] = headers['token-type'];
      newSession['authenticated']['uid'][0] = headers['uid'];
      newSession['authenticated']['client'][0] = headers['client'];
      this.get('session.store').persist(newSession);
    } else if (status == 401) {
      this.get('session').invalidate();
    }
    return this._super(...arguments);
  }
});

And this is my authenticator code

// `/authenticators/authenticator_devise.js`
import DeviseAuthenticator from 'ember-simple-auth/authenticators/devise';
import Ember from 'ember';
import config from '../config/environment';
const { RSVP, isEmpty, run } = Ember;

export default DeviseAuthenticator.extend({
  session: Ember.inject.service('session'),
  serverTokenEndpoint: `${config.apiHost}/auth/sign_in`,


  restore(data){
    return new RSVP.Promise((resolve, reject) => {
      if (!isEmpty(data.accessToken) && !isEmpty(data.expiry) &&
          !isEmpty(data.tokenType) && !isEmpty(data.uid) && !isEmpty(data.client)) {
        console(data);
        resolve(data);
      } else {
        reject();
      }
    });
  },

  authenticate(identification, password) {
  //console(`${config.apiHost}/${config.apiNamespace}/auth/sign_in`);
   return new RSVP.Promise((resolve, reject) => {
      const { identificationAttributeName } = this.getProperties('identificationAttributeName');
      const data = { password };
      data[identificationAttributeName] = identification;
      this.makeRequest(data).then(function(response) {
        if(response.status != 401) {
          var result = {
            accessToken: response.headers.map['access-token'],
            expiry: response.headers.map['expiry'],
            tokenType: response.headers.map['token-type'],
            uid: response.headers.map['uid'],
            client: response.headers.map['client']
          };
          run(null, resolve, result);
        }else {
          alert("Usuario o password incorrecto");
        }

      }, function(xhr) {
        run(null, reject, xhr.responseJSON || xhr.responseText);
      });
    });
  },

  invalidate() {
    return new RSVP.Promise((resolve, reject)=> {
      Ember.$.ajax({
        type: "DELETE",
        url: `${config.apiHost}/auth/sign_out`,
        headers: {
          'uid': 'prueba@prueba.com',
        }
      }).then(function(response) {
        Ember.run(function() {
          resolve(response);
        });
      }, function(xhr, status, error) {
        Ember.run(function() {
          reject(xhr.responseJSON || xhr.responseText);
        });
      });
    });

  }

});

and my authorized

// `/authorizers/authorizer_devise.js`

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

const { service } = Ember.inject;

export default Base.extend({
  session: service('session'),
  authorize(data, block) {
    console("entro a authorize");
    const accessToken = data.accessToken[0];
    const expiry = data.expiry[0];
    const tokenType = data.tokenType[0];
    const uid = data.uid[0];
    const client = data.client[0];
    if (this.get('session.isAuthenticated') && !Ember.isEmpty(accessToken)) {
      block('Authorization', `Bearer ${accessToken}`);
      block('access-token', accessToken);
      block('expiry', expiry);
      block('token-type', tokenType);
      block('uid', uid);
      block('client', client);
    }
  }
});

And in my logi-component i invoque the authenticator

import Ember from 'ember';

export default Ember.Component.extend({
    session: Ember.inject.service('session'),
    actions:{
        authenticate(){

            this.get('session').authenticate('authenticator:authenticator-devise', this.get('email'), this.get('password'));


        }
    }
});




Aucun commentaire:

Enregistrer un commentaire