Howl Jenkins
Howl Jenkins

Reputation: 323

Refresh access token ember

My app access token expires every 10 minutes. I'm using ember simple auth for authentication, but is a custom authenticator, because I have more than just username and password fields. I wanted to use ember-simple-auth-token, but calling to authorization:jwt in my adapter, it doesn't work, apparently is because I'm not using it's authenticator.

So, I was considering to use a service that renew the token every ten minutes, something like this http://www.davekerr.co/programming/2015/05/17/add-a-polling-service-to-your-ember-app/, so my question is how to overwrite the sessionData token.

My custom authorizer file code looks like this:

import Ember from 'ember';
import Base from 'ember-simple-auth/authorizers/base';
export default Base.extend({
    authorize(sessionData, block) {
        if (!Ember.isEmpty(sessionData.token)) {
          block('Authorization',  'Bearer '+sessionData.token);
          block('Content-Type',  'application/json');
        }
    }
});

Upvotes: 2

Views: 1278

Answers (1)

marcoow
marcoow

Reputation: 4062

You should do that in your custom authenticator as the authenticator is responsible for acquiring and managing tokens and authentication state in general. You can simply schedule a timer that updates the token some time after initial authentication. Once the token has been refreshed the authenticator can trigger the sessionDataUpdated event that the session will automatically handle.

Check the implementation of the OAuth2PasswortGrantAuthenticator for an example of how to update tokens from the authenticator.

Upvotes: 3

Related Questions