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

Commit

Permalink
fix(baseConfig): logoutOnInvalidtoken -> logoutOnInvalidToken
Browse files Browse the repository at this point in the history
The parameter had a typo. I deprecated the old version and added tests to ensure that both versions work fine.
  • Loading branch information
doktordirk committed Apr 23, 2018
1 parent 3c51df6 commit 16bb54b
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 2 deletions.
4 changes: 4 additions & 0 deletions gulpfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ require('require-dir')('node_modules/spoonx-tools/build-plugin/tasks');
// the testing express server can be imported and routes added
var app = require('./node_modules/spoonx-tools/build-plugin/tasks/server').app;

// unauthorized test path
app.all('/unauthorized', function(req, res) {
res.sendStatus(401);
});

// default: all routes, all methods
app.all('*', function(req, res) {
Expand Down
21 changes: 20 additions & 1 deletion src/baseConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export class BaseConfig {
// The token name used in the header of API requests that require authentication
authTokenType = 'Bearer';
// Logout when the token is invalidated by the server
logoutOnInvalidtoken = false;
logoutOnInvalidToken = false;
// The the property from which to get the access token after a successful login or signup. Can also be dotted eg "accessTokenProp.accessTokenName"
accessTokenProp = 'access_token';

Expand Down Expand Up @@ -331,6 +331,10 @@ export class BaseConfig {
* @deprecated
*/
_tokenPrefix = 'aurelia';
/**
* @deprecated
*/
_logoutOnInvalidtoken = false;

/* deprecated methods and parameters */
/**
Expand Down Expand Up @@ -430,6 +434,21 @@ export class BaseConfig {
set _current(_) {
throw new Error('Setter BaseConfig._current has been removed. Use BaseConfig directly instead.');
}

/**
* @param {string} logoutOnInvalidtoken
* @deprecated
*/
set logoutOnInvalidtoken(logoutOnInvalidtoken) {
logger.warn('BaseConfig.logoutOnInvalidtoken is obsolete. Use BaseConfig.logoutOnInvalidToken instead.');
this._logoutOnInvalidtoken = logoutOnInvalidtoken;
this.logoutOnInvalidToken = logoutOnInvalidtoken;

return logoutOnInvalidtoken;
}
get logoutOnInvalidtoken() {
return this._logoutOnInvalidtoken;
}
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/fetchClientConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class FetchConfig {
return reject(response);
}
// logout when server invalidated the authorization token but the token itself is still valid
if (this.config.httpInterceptor && this.config.logoutOnInvalidtoken && !this.authService.isTokenExpired()) {
if (this.config.httpInterceptor && this.config.logoutOnInvalidToken && !this.authService.isTokenExpired()) {
return reject(this.authService.logout());
}
// resolve unexpected authorization errors (not a managed request or token not expired)
Expand Down
71 changes: 71 additions & 0 deletions test/fetchClientConfig.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,15 @@ import {Config} from 'aurelia-api';
import {FetchConfig} from '../src/fetchClientConfig';
import {AuthService} from '../src/authService';

const tokenFuture = {
payload: {
name : 'tokenFuture',
admin: true,
exp : '2460017154'
},
jwt: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJuYW1lIjoidG9rZW5GdXR1cmUiLCJhZG1pbiI6dHJ1ZSwiZXhwIjoiMjQ2MDAxNzE1NCJ9.iHXLzWGY5U9WwVT4IVRLuKTf65XpgrA1Qq_Jlynv6bc'
};

function getContainer() {
let container = new Container();
let config = container.get(Config);
Expand Down Expand Up @@ -255,6 +264,68 @@ describe('FetchConfig', function() {
done();
});
});

it('Should not logout on invalid token (default)', function(done) {
let client = new HttpClient();

client.baseUrl = 'http://localhost:1927/';
authService.setResponseObject({access_token: tokenFuture.jwt});
fetchConfig.configure(client);

client.fetch('unauthorized')
.then(response => {
expect(authService.isAuthenticated()).toBe(true);

done();
})
.catch(err => {
expect(true).toBe(false);

done();
});
});

it('Should logout on invalid token (logoutOnInvalidToken = true)', function(done) {
let client = new HttpClient();

client.baseUrl = 'http://localhost:1927/';
authService.setResponseObject({access_token: tokenFuture.jwt});
authService.config.logoutOnInvalidToken = true;
fetchConfig.configure(client);

client.fetch('unauthorized')
.then(response => {
expect(true).toBe(false);

done();
})
.catch(err => {
expect(authService.isAuthenticated()).toBe(false);

done();
});
});

it('Should logout on invalid token (logoutOnInvalidtoken = true) (deprecated)', function(done) {
let client = new HttpClient();

client.baseUrl = 'http://localhost:1927/';
authService.setResponseObject({access_token: tokenFuture.jwt});
authService.config.logoutOnInvalidtoken = true;
fetchConfig.configure(client);

client.fetch('unauthorized')
.then(response => {
expect(true).toBe(false);

done();
})
.catch(err => {
expect(authService.isAuthenticated()).toBe(false);

done();
});
});
});

authService.accessToken = null;
Expand Down

0 comments on commit 16bb54b

Please sign in to comment.