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

Commit

Permalink
fix(authentication): consistent throw if token not found
Browse files Browse the repository at this point in the history
  • Loading branch information
doktordirk committed May 27, 2016
1 parent 877fa50 commit 41454e3
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/authentication.js
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,12 @@ export class Authentication {
}

if (typeof responseTokenProp === 'object') {
const tokenRootData = tokenRoot && tokenRoot.split('.').reduce(function(o, x) { return o[x]; }, responseTokenProp);
return tokenRootData ? tokenRootData[tokenName] : responseTokenProp[tokenName];
const tokenRootData = tokenRoot && tokenRoot.split('.').reduce((o, x) => o[x], responseTokenProp);
const token = tokenRootData ? tokenRootData[tokenName] : responseTokenProp[tokenName];

if (!token) throw new Error('Token not found in response');

return token;
}

const token = response[tokenName] === undefined ? null : response[tokenName];
Expand Down
9 changes: 9 additions & 0 deletions test/authentication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,15 @@ describe('Authentication', () => {
)).toBe('some');
});

it('Should throw if token not found in nested', () => {
const fail = () => authentication.getTokenFromResponse(
{tokenProp: {wrongTokenName: 'some'}},
'tokenProp',
'tokenName'
);
expect(fail).toThrow();
});

it('Should return token if response has a string in tokenName in tokenRoot of tokenProp', () => {
expect(authentication.getTokenFromResponse(
{tokenProp: {tokenRoot1: {tokenRoot2: {tokenName: 'some'}}}},
Expand Down

0 comments on commit 41454e3

Please sign in to comment.