0
votes

I'm trying to implement JWT authentication into my Ember application using the ember-simple-auth and ember-simple-auth-token modules by following their README files on GitHub. I got the authentication part to work pretty easily. I can send credentials to the authentication endpoint, and get a token back. But then when I try to implement authorization through the JSONAPIAdapter, I get the following error

Error: Failed to create an instance of 'authorizer:token'. Most likely an improperly defined class or an invalid module export.

I'm using Ember 2.12 and have the following setup:

app/adapters/application.js

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
  host: 'http://localhost:8000',
  authorizer: 'authorizer:token'
});

app/routes/users.js

import Ember from 'ember';

export default Ember.Route.extend({
  session: Ember.inject.service('session'),
  model() {
    return this.store.findAll('user');
  }
});

package.json

"ember-cli-simple-auth": "^0.8.0",
"ember-simple-auth": "^1.2.2",
"ember-simple-auth-token": "^2.1.0",
1

1 Answers

0
votes

I don't know how is your authenticator, but you can try something like this for your authorizer:

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

export default Base.extend({
  session: Ember.inject.service(),

  authorize(data, block) {
    if (Ember.testing) {
      block('Authorization', 'Bearer beyonce');
    }
    const { token } = data
    if (this.get('session.isAuthenticated') && token) {
      block('Authorization', `Bearer ${token}`);
    }
  }
});