Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

custom config tokenName in default tokenGetter #230

Merged
merged 2 commits into from Dec 14, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions angular2-jwt.spec.ts
Expand Up @@ -45,6 +45,16 @@ describe('AuthConfig', ()=> {
expect(config.tokenGetter).toBeDefined();
expect(config.tokenGetter()).toBe("this is a token");
});

it('should use custom token name in default tokenGetter', ()=> {
const configExpected = { tokenName: 'Token' };
const token = 'token';
const config = new AuthConfig(configExpected).getConfig();
localStorage.setItem(configExpected.tokenName, token);
expect(config).toBeDefined();
expect(config.tokenName).toBe(configExpected.tokenName);
expect(config.tokenGetter()).toBe(token);
});

});

Expand Down
6 changes: 5 additions & 1 deletion angular2-jwt.ts
Expand Up @@ -58,6 +58,10 @@ export class AuthConfig {
} else {
this._config.headerPrefix = AuthConfigConsts.HEADER_PREFIX_BEARER;
}

if (config.tokenName && !config.tokenGetter) {
this._config.tokenGetter = () => localStorage.getItem(config.tokenName) as string;
}
}

public getConfig():IAuthConfig {
Expand Down Expand Up @@ -345,4 +349,4 @@ function objectAssign(target: any, ...source: any[]) {
}
}
return to;
}
}