Skip to content

Commit 964c39e

Browse files
authored
Merge pull request #274 from fenichelar/master
Add token adapter mixin, fixes #273
2 parents facc3be + 4c347b6 commit 964c39e

File tree

7 files changed

+4266
-1827
lines changed

7 files changed

+4266
-1827
lines changed

README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,24 @@ To debug JSON Web Token issues, see [jwt][jwt].
9999

100100
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].
101101

102+
### Adapter Mixin
103+
104+
In order to send the token with all API requests made to the server, the token adapter mixin should be used:
105+
106+
```js
107+
// app/adapters/application.js
108+
import DS from 'ember-data';
109+
import TokenAdapterMixin from 'ember-simple-auth-token/mixins/token-adapter';
110+
111+
export default DS.JSONAPIAdapter.extend(TokenAdapterMixin);
112+
```
113+
114+
The mixin will add the header to each API request:
115+
116+
```
117+
Authorization: Bearer <token>
118+
```
119+
102120
### Authorizer Mixin
103121

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

addon/mixins/token-adapter.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import Mixin from '@ember/object/mixin';
2+
import { inject } from '@ember/service';
3+
import { get, computed } from '@ember/object';
4+
import { isEmpty } from '@ember/utils';
5+
import config from 'ember-get-config';
6+
import DataAdapterMixin from 'ember-simple-auth/mixins/data-adapter-mixin';
7+
8+
/**
9+
Adapter Mixin that works with token-based authentication like JWT.
10+
11+
@class TokenAdapter
12+
@module ember-simple-auth-token/mixins/token-adapter
13+
@extends Ember.Mixin
14+
*/
15+
export default Mixin.create(DataAdapterMixin, {
16+
session: inject('session'),
17+
18+
/**
19+
@method init
20+
*/
21+
init() {
22+
this._super(...arguments);
23+
const conf = config['ember-simple-auth-token'] || {};
24+
this.tokenPropertyName = conf.tokenPropertyName || 'token';
25+
this.authorizationHeaderName = conf.authorizationHeaderName || 'Authorization';
26+
this.authorizationPrefix = conf.authorizationPrefix === '' ? '' : conf.authorizationPrefix || 'Bearer ';
27+
},
28+
29+
/*
30+
Adds the `token` property from the session to the `authorizationHeaderName`:
31+
*/
32+
headers: computed('session.data.authenticated', function() {
33+
const data = this.get('session.data.authenticated');
34+
const token = get(data, this.get('tokenPropertyName'));
35+
const prefix = this.get('authorizationPrefix');
36+
const header = this.get('authorizationHeaderName');
37+
38+
if (this.get('session.isAuthenticated') && !isEmpty(token)) {
39+
return {
40+
[header]: `${prefix}${token}`
41+
};
42+
} else {
43+
return {};
44+
}
45+
})
46+
});

0 commit comments

Comments
 (0)