Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit

Permalink
feat(baseConfig): standardize access token option names (breaking)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: This aligns access token option names with the refresh token option names. The option changes are as follows:

```
tokenStorage      => accessTokenStorage
responseTokenProp => accessTokenProp
tokenName         => accessTokenName
tokenRoot         => accessTokenRoot
```
  • Loading branch information
AdamWillden authored and doktordirk committed Apr 9, 2016
1 parent c8885d7 commit 29d22c5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 32 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -323,18 +323,18 @@ unlinkMethod: 'get',
authHeader: 'Authorization',
// The token name used in the header of API requests that require authentication
authToken: 'Bearer',
// The property name used when storing the token locally
tokenStorage: 'aurelia_access_token',
// The the property from which to get the authentication token after a successful login or signup
responseTokenProp: 'access_token',
// The property name used when storing the access token locally
accessTokenStorage: 'aurelia_access_token',
// The the property from which to get the access token after a successful login or signup
accessTokenProp: 'access_token',

// If the property defined by `responseTokenProp` is an object:
// If the property defined by `accessTokenProp` is an object:
// ------------------------------------------------------------

//This is the property from which to get the token `{ "responseTokenProp": { "tokenName" : '...' } }`
tokenName: 'token',
// This allows the token to be a further object deeper `{ "responseTokenProp": { "tokenRoot" : { "tokenName" : '...' } } }`
tokenRoot: false,
//This is the property from which to get the token `{ "accessTokenProp": { "accessTokenName" : '...' } }`
accessTokenName: 'token',
// This allows the token to be a further object deeper `{ "accessTokenProp": { "accessTokenRoot" : { "accessTokenName" : '...' } } }`
accessTokenRoot: false,


// Refresh Token Options
Expand Down
28 changes: 14 additions & 14 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ export class Authentication {
}

getToken() {
return this.storage.get(this.config.tokenStorage);
return this.storage.get(this.config.accessTokenStorage);
}

getRefreshToken() {
return this.storage.get(this.config.refreshTokenStorage);
}

getPayload() {
let token = this.storage.get(this.config.tokenStorage);
let token = this.storage.get(this.config.accessTokenStorage);

if (token && token.split('.').length === 3) {
let base64Url = token.split('.')[1];
Expand All @@ -53,7 +53,7 @@ export class Authentication {
}

setTokenFromResponse(response, redirect) {
let accessToken = response && response[this.config.responseTokenProp];
let accessToken = response && response[this.config.accessTokenProp];
let token;

if (accessToken) {
Expand All @@ -65,20 +65,20 @@ export class Authentication {
}

if (!token && response) {
token = this.config.tokenRoot && response[this.config.tokenRoot]
? response[this.config.tokenRoot][this.config.tokenName]
: response[this.config.tokenName];
token = this.config.accessTokenRoot && response[this.config.accessTokenRoot]
? response[this.config.accessTokenRoot][this.config.accessTokenName]
: response[this.config.accessTokenName];
}

if (!token) {
let tokenPath = this.config.tokenRoot
? this.config.tokenRoot + '.' + this.config.tokenName
: this.config.tokenName;
let accessTokenPath = this.config.accessTokenRoot
? this.config.accessTokenRoot + '.' + this.config.accessTokenName
: this.config.accessTokenName;

throw new Error('Expecting a token named "' + tokenPath + '" but instead got: ' + JSON.stringify(response));
throw new Error('Expecting a token named "' + accessTokenPath + '" but instead got: ' + JSON.stringify(response));
}

this.storage.set(this.config.tokenStorage, token);
this.storage.set(this.config.accessTokenStorage, token);

if (this.config.loginRedirect && !redirect) {
window.location.href = this.config.loginRedirect;
Expand Down Expand Up @@ -117,15 +117,15 @@ export class Authentication {
}

removeToken() {
this.storage.remove(this.config.tokenStorage);
this.storage.remove(this.config.accessTokenStorage);
}

removeRefreshToken() {
this.storage.remove(this.config.refreshTokenStorage);
}

isAuthenticated() {
let token = this.storage.get(this.config.tokenStorage);
let token = this.storage.get(this.config.accessTokenStorage);

// There's no token, so user is not authenticated.
if (!token) {
Expand Down Expand Up @@ -167,7 +167,7 @@ export class Authentication {

logout(redirect) {
return new Promise(resolve => {
this.storage.remove(this.config.tokenStorage);
this.storage.remove(this.config.accessTokenStorage);
this.storage.remove(this.config.refreshTokenStorage);

if (this.config.logoutRedirect && !redirect) {
Expand Down
18 changes: 9 additions & 9 deletions src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,18 @@ export class BaseConfig {
authHeader: 'Authorization',
// The token name used in the header of API requests that require authentication
authToken: 'Bearer',
// The property name used when storing the token locally
tokenStorage: 'aurelia_access_token',
// The the property from which to get the authentication token after a successful login or signup
responseTokenProp: 'access_token',
// The property name used when storing the access token locally
accessTokenStorage: 'aurelia_access_token',
// The the property from which to get the access token after a successful login or signup
accessTokenProp: 'access_token',

// If the property defined by `responseTokenProp` is an object:
// If the property defined by `accessTokenProp` is an object:
// ------------------------------------------------------------

//This is the property from which to get the token `{ "responseTokenProp": { "tokenName" : '...' } }`
tokenName: 'token',
// This allows the token to be a further object deeper `{ "responseTokenProp": { "tokenRoot" : { "tokenName" : '...' } } }`
tokenRoot: false,
//This is the property from which to get the token `{ "accessTokenProp": { "accessTokenName" : '...' } }`
accessTokenName: 'token',
// This allows the token to be a further object deeper `{ "accessTokenProp": { "accessTokenRoot" : { "accessTokenName" : '...' } } }`
accessTokenRoot: false,


// Refresh Token Options
Expand Down

0 comments on commit 29d22c5

Please sign in to comment.