Skip to content

Commit

Permalink
Merge pull request #274 from fenichelar/master
Browse files Browse the repository at this point in the history
Add token adapter mixin, fixes #273
  • Loading branch information
fenichelar authored Jul 7, 2020
2 parents facc3be + 4c347b6 commit 964c39e
Show file tree
Hide file tree
Showing 7 changed files with 4,266 additions and 1,827 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ To debug JSON Web Token issues, see [jwt][jwt].

The JSON Web Token authenticator supports both separate access tokens and refresh tokens. By specifying the `tokenPropertyName` and the `refreshTokenPropertyName` to the same value, the same token will be used for both access and refresh requests. For more information about refresh tokens, see [this blog][blog-refresh-token].

### Adapter Mixin

In order to send the token with all API requests made to the server, the token adapter mixin should be used:

```js
// app/adapters/application.js
import DS from 'ember-data';
import TokenAdapterMixin from 'ember-simple-auth-token/mixins/token-adapter';

export default DS.JSONAPIAdapter.extend(TokenAdapterMixin);
```

The mixin will add the header to each API request:

```
Authorization: Bearer <token>
```

### Authorizer Mixin

In order to send the token with all API requests made to the server, the token authorizer mixin should be used:
Expand Down
46 changes: 46 additions & 0 deletions addon/mixins/token-adapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import Mixin from '@ember/object/mixin';
import { inject } from '@ember/service';
import { get, computed } from '@ember/object';
import { isEmpty } from '@ember/utils';
import config from 'ember-get-config';
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';

/**
Adapter Mixin that works with token-based authentication like JWT.
@class TokenAdapter
@module ember-simple-auth-token/mixins/token-adapter
@extends Ember.Mixin
*/
export default Mixin.create(DataAdapterMixin, {
session: inject('session'),

/**
@method init
*/
init() {
this._super(...arguments);
const conf = config['ember-simple-auth-token'] || {};
this.tokenPropertyName = conf.tokenPropertyName || 'token';
this.authorizationHeaderName = conf.authorizationHeaderName || 'Authorization';
this.authorizationPrefix = conf.authorizationPrefix === '' ? '' : conf.authorizationPrefix || 'Bearer ';
},

/*
Adds the `token` property from the session to the `authorizationHeaderName`:
*/
headers: computed('session.data.authenticated', function() {
const data = this.get('session.data.authenticated');
const token = get(data, this.get('tokenPropertyName'));
const prefix = this.get('authorizationPrefix');
const header = this.get('authorizationHeaderName');

if (this.get('session.isAuthenticated') && !isEmpty(token)) {
return {
[header]: `${prefix}${token}`
};
} else {
return {};
}
})
});
Loading

0 comments on commit 964c39e

Please sign in to comment.