Ember Simple Auth - Authorizer not being setup when the session is restored

310 Views Asked by At

The code below currently works but If I remove the line with _setup then the outgoing requests don't have the Authorization header.

It doesn't feel like I should be using the _setup function as it isn't in the documentation.

What am I doing wrong?

I'm using the latest version of Ember and Ember-Simple-Auth with the Oauth Password Grant.

Ember.getOwner(this).lookup('authenticator:custom').restore(token).then(() => {
    Ember.getOwner(this).lookup('session:main')._setup('authenticator:custom', token, true);
});
2

There are 2 best solutions below

2
On BEST ANSWER

Restore is a convenience function that runs on app startup and store changes. As far as I know, it's not intended to be called manually, but should be firing automatically on app boot and restoring from session data. Whatever you are trying to do by calling restore manually, you could possibly handle inside the authenticate hook instead, passing the token as an argument.

A canonical call to a custom authenticator should look something like

  session: Ember.inject.service('session'),
  someFunction() {
    let token = this.get('tokenSavedSomewhere')
    this.get('session').authenticate('authenticator:custom', token).catch((reason) => {
      console.log('Reject reason', reason)
    });
  },
0
On

In case it helps anyone, this is what I ended up doing.

routes/application.js (Code snippet)

this.get('session').authenticate('authenticator:custom', token).catch((reason) => {
  console.log('Reject reason', reason)
})    

authenticators/application.js

export default Authenticator.extend({
  authenticate(token) {
    return this.restore(token);
  }
});