Skip to content

Commit

Permalink
Added global options for setting custom global headers
Browse files Browse the repository at this point in the history
  • Loading branch information
neroniaky committed Oct 7, 2016
1 parent 92d0cf7 commit a813313
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

## v0.1.15
**Featues:**
- Added global options for setting custom global headers

## v0.1.14
**Bugfixes:**
- Fixes auth data retrival from http paramater, fixes #29
Expand Down
15 changes: 14 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,14 @@ constructor(private _tokenService: Angular2TokenService) {
resetPasswordPath: 'auth/password',
resetPasswordCallback: window.location.href,
userTypes: null
userTypes: null,
globalOptions: {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
});
}
```
Expand All @@ -119,6 +126,12 @@ constructor(private _tokenService: Angular2TokenService) {
| `resetPasswordPath?: string` | Sets path for password reset |
| `resetPasswordCallback?: string` | Sets the path user are redirected to after email confirmation for password reset |
| `userTypes?: UserTypes[]` | Allows the configuration of multiple user types (see [Multiple User Types](#multiple-user-types)) |
| `globalOptions?: GlobalOptions` | Allows the configuration of global options (see below) |
### Global Options
| Options | Description |
| ------------------------------------- | ----------------------------------------------- |
| `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 |
Further information on paths/routes can be found at
[devise token auth](https://github.com/lynndylanhurley/devise_token_auth#usage-tldr)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "angular2-token",
"version": "0.1.14",
"version": "0.1.15",
"description": "Angular2 service for token based authentication",
"main": "./angular2-token.js",
"typings": "./angular2-token.d.ts",
Expand Down
6 changes: 6 additions & 0 deletions src/angular2-token.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ export interface OAuthPaths {
github?: string;
}

export interface GlobalOptions {
headers?: { [key:string]: string; }
}

export interface Angular2TokenOptions {
apiPath?: string;

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

oAuthPaths?: OAuthPaths;

globalOptions?: GlobalOptions;
}
45 changes: 22 additions & 23 deletions src/angular2-token.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -82,10 +82,17 @@ export class Angular2TokenService implements CanActivate {

oAuthPaths: {
github: 'auth/github'
},

globalOptions: {
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
}
}
};

this._options = Object.assign(defaultOptions, options);
this._options = (<any>Object).assign(defaultOptions, options);

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

let headers: Headers;
let baseRequestOptions: RequestOptions;
let mergedRequestOptions: RequestOptions;

// Set Headers
if (this._currentAuthData != null)
headers = new Headers({
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
'Accept': 'application/json',
let baseHeaders: { [key:string]: string; } = this._options.globalOptions.headers;

// Merge auth headers to request if set
if (this._currentAuthData != null) {
(<any>Object).assign(baseHeaders, {
'access-token': this._currentAuthData.accessToken,
'client': this._currentAuthData.client,
'expiry': this._currentAuthData.expiry,
'token-type': this._currentAuthData.tokenType,
'uid': this._currentAuthData.uid
});
else
headers = new Headers({
'Content-Type': 'application/json', // ToDo: Add to RequestOptions if available
'Accept': 'application/json'
'client': this._currentAuthData.client,
'expiry': this._currentAuthData.expiry,
'token-type': this._currentAuthData.tokenType,
'uid': this._currentAuthData.uid
});
}

// Construct Default Request Options
baseRequestOptions = new RequestOptions({
headers: headers
})
headers: new Headers(baseHeaders)
});

// Merge standard and custom RequestOptions
mergedRequestOptions = baseRequestOptions.merge(requestOptions);
baseRequestOptions = baseRequestOptions.merge(requestOptions);

let response = this._http.request(new Request(mergedRequestOptions)).share();
let response = this._http.request(new Request(baseRequestOptions)).share();

this._handleResponse(response);

Expand Down

0 comments on commit a813313

Please sign in to comment.