WebDevDude
WebDevDude

Reputation: 859

Ember Simple Auth not setting the Bearer Token

For whatever reason, my authentication seems to be working correctly for logging in, but then I can't got to any routes that use Ember Data. The Bearer Token seems to not be getting set.

Here is my login-form component

import Ember from 'ember';

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

  actions: {
    authenticate() {
      let { identification, password } = this.getProperties('identification', 'password');
      this.get('session').authenticate('authenticator:oauth2', identification, password)
      .catch((reason) => {
        this.set('errorMessage', reason.error);
        console.log(reason.error);
      });
    }
  }

});

This is my authorizer (lives in app/authorizers/application.js)

import OAuth2Bearer from 'ember-simple-auth/authorizers/oauth2-bearer';

export default OAuth2Bearer.extend();

and this is my authenticator (lives in app/authenticators/oauth2.js)

import OAuth2PasswordGrant from 'ember-simple-auth/authenticators/oauth2-password-grant';
import config from 'contacts/config/environment';

export default OAuth2PasswordGrant.extend({
  serverTokenEndpoint: config.APP.host + '/' + config.APP.namespace + '/authenticate',
});

If anyone see's that I'm missing something, please let me know. Thank you!

Upvotes: 1

Views: 432

Answers (2)

Kevin Jhangiani
Kevin Jhangiani

Reputation: 1607

If you are using ember-data, you are missing the DataAdapterMixin on your application adapter.

See the example here:

http://log.simplabs.com/post/131698328145/updating-to-ember-simple-auth-10

The relevant code is:

// app/adapters/application.js
import DS from 'ember-data';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

export default DS.JSONAPIAdapter.extend(DataAdapterMixin, {
  authorizer: 'authorizer:application'
});

The DataAdapterMixin is what injects the token into the request header, so if authentication is working, but subsequent API requests are not, the problem is likely somewhere in this file.

If you are not using ember-data, then you will have to setup the adapter yourself to manually inject the token into auth requests. You can see some examples on that link above.

EDIT: Fixed the name of the authorizer, yours lives in authorizers/application.js, so the authorizer is 'authorizer:application'

Upvotes: 0

cswright
cswright

Reputation: 151

I figured I'd write this up as an answer instead because everything else looks good to me

session: Ember.inject.service('session')

Although if this were the case it wouldn't make sense that you could call authenticate... hmm, you said the log in seems to work?

Upvotes: 0

Related Questions