Skip to content

Commit a813313

Browse files
committed
Added global options for setting custom global headers
1 parent 92d0cf7 commit a813313

File tree

5 files changed

+47
-25
lines changed

5 files changed

+47
-25
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Change Log
22

3+
## v0.1.15
4+
**Featues:**
5+
- Added global options for setting custom global headers
6+
37
## v0.1.14
48
**Bugfixes:**
59
- Fixes auth data retrival from http paramater, fixes #29

README.md

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,14 @@ constructor(private _tokenService: Angular2TokenService) {
100100
resetPasswordPath: 'auth/password',
101101
resetPasswordCallback: window.location.href,
102102
103-
userTypes: null
103+
userTypes: null,
104+
105+
globalOptions: {
106+
headers: {
107+
'Content-Type': 'application/json',
108+
'Accept': 'application/json'
109+
}
110+
}
104111
});
105112
}
106113
```
@@ -119,6 +126,12 @@ constructor(private _tokenService: Angular2TokenService) {
119126
| `resetPasswordPath?: string` | Sets path for password reset |
120127
| `resetPasswordCallback?: string` | Sets the path user are redirected to after email confirmation for password reset |
121128
| `userTypes?: UserTypes[]` | Allows the configuration of multiple user types (see [Multiple User Types](#multiple-user-types)) |
129+
| `globalOptions?: GlobalOptions` | Allows the configuration of global options (see below) |
130+
131+
### Global Options
132+
| Options | Description |
133+
| ------------------------------------- | ----------------------------------------------- |
134+
| `headers?: { [key:string]: string; }` | Define custom global headers as hashmap. Be careful when overwriting the default options, `devise token auth` will refuse requests without the `Content-Type`-Header set |
122135
123136
Further information on paths/routes can be found at
124137
[devise token auth](https://github.com/lynndylanhurley/devise_token_auth#usage-tldr)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "angular2-token",
3-
"version": "0.1.14",
3+
"version": "0.1.15",
44
"description": "Angular2 service for token based authentication",
55
"main": "./angular2-token.js",
66
"typings": "./angular2-token.d.ts",

src/angular2-token.model.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ export interface OAuthPaths {
2727
github?: string;
2828
}
2929

30+
export interface GlobalOptions {
31+
headers?: { [key:string]: string; }
32+
}
33+
3034
export interface Angular2TokenOptions {
3135
apiPath?: string;
3236

@@ -48,4 +52,6 @@ export interface Angular2TokenOptions {
4852
userTypes?: UserType[];
4953

5054
oAuthPaths?: OAuthPaths;
55+
56+
globalOptions?: GlobalOptions;
5157
}

src/angular2-token.service.ts

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,17 @@ export class Angular2TokenService implements CanActivate {
8282

8383
oAuthPaths: {
8484
github: 'auth/github'
85+
},
86+
87+
globalOptions: {
88+
headers: {
89+
'Content-Type': 'application/json',
90+
'Accept': 'application/json'
91+
}
8592
}
8693
};
8794

88-
this._options = Object.assign(defaultOptions, options);
95+
this._options = (<any>Object).assign(defaultOptions, options);
8996

9097
this._tryLoadAuthData();
9198
}
@@ -261,36 +268,28 @@ export class Angular2TokenService implements CanActivate {
261268
// Construct and send Http request
262269
sendHttpRequest(requestOptions: RequestOptions): Observable<Response> {
263270

264-
let headers: Headers;
265271
let baseRequestOptions: RequestOptions;
266-
let mergedRequestOptions: RequestOptions;
267-
268-
// Set Headers
269-
if (this._currentAuthData != null)
270-
headers = new Headers({
271-
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
272-
'Accept': 'application/json',
272+
let baseHeaders: { [key:string]: string; } = this._options.globalOptions.headers;
273+
274+
// Merge auth headers to request if set
275+
if (this._currentAuthData != null) {
276+
(<any>Object).assign(baseHeaders, {
273277
'access-token': this._currentAuthData.accessToken,
274-
'client': this._currentAuthData.client,
275-
'expiry': this._currentAuthData.expiry,
276-
'token-type': this._currentAuthData.tokenType,
277-
'uid': this._currentAuthData.uid
278-
});
279-
else
280-
headers = new Headers({
281-
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
282-
'Accept': 'application/json'
278+
'client': this._currentAuthData.client,
279+
'expiry': this._currentAuthData.expiry,
280+
'token-type': this._currentAuthData.tokenType,
281+
'uid': this._currentAuthData.uid
283282
});
283+
}
284284

285-
// Construct Default Request Options
286285
baseRequestOptions = new RequestOptions({
287-
headers: headers
288-
})
286+
headers: new Headers(baseHeaders)
287+
});
289288

290289
// Merge standard and custom RequestOptions
291-
mergedRequestOptions = baseRequestOptions.merge(requestOptions);
290+
baseRequestOptions = baseRequestOptions.merge(requestOptions);
292291

293-
let response = this._http.request(new Request(mergedRequestOptions)).share();
292+
let response = this._http.request(new Request(baseRequestOptions)).share();
294293

295294
this._handleResponse(response);
296295

0 commit comments

Comments
 (0)