jeudi 23 juin 2016

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

I'm using ember-simple-auth to manage my app authentication.

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.

And another question, when is it called the authorizer?




Aucun commentaire:

Enregistrer un commentaire