-
Notifications
You must be signed in to change notification settings - Fork 140
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #274 from fenichelar/master
Add token adapter mixin, fixes #273
- Loading branch information
Showing
7 changed files
with
4,266 additions
and
1,827 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {}; | ||
} | ||
}) | ||
}); |
Oops, something went wrong.