angular2-jwt
Advanced tools
Comparing version 0.1.11 to 0.1.12
@@ -9,2 +9,3 @@ import { Http, Request, RequestOptionsArgs, Response } from 'angular2/http'; | ||
noJwtError: boolean; | ||
globalHeaders: Array<Object>; | ||
} | ||
@@ -21,2 +22,4 @@ /** | ||
noJwtError: boolean; | ||
noTokenScheme: boolean; | ||
globalHeaders: Array<Object>; | ||
constructor(config?: any); | ||
@@ -29,2 +32,4 @@ getConfig(): { | ||
noJwtError: boolean; | ||
emptyHeaderPrefix: boolean; | ||
globalHeaders: Object[]; | ||
}; | ||
@@ -40,2 +45,3 @@ } | ||
constructor(options: AuthConfig, http: Http); | ||
setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs): void; | ||
request(url: string | Request, options?: RequestOptionsArgs): Observable<Response>; | ||
@@ -64,1 +70,2 @@ private requestHelper(requestArgs, additionalOptions); | ||
export declare function tokenNotExpired(tokenName?: string, jwt?: string): boolean; | ||
export declare const AUTH_PROVIDERS: any; |
@@ -25,2 +25,5 @@ "use strict"; | ||
} | ||
else if (this.config.noTokenScheme) { | ||
this.headerPrefix = ''; | ||
} | ||
else { | ||
@@ -32,2 +35,3 @@ this.headerPrefix = 'Bearer '; | ||
this.tokenGetter = this.config.tokenGetter || (function () { return localStorage.getItem(_this.tokenName); }); | ||
this.globalHeaders = this.config.globalHeaders || null; | ||
} | ||
@@ -40,3 +44,5 @@ AuthConfig.prototype.getConfig = function () { | ||
tokenGetter: this.tokenGetter, | ||
noJwtError: this.noJwtError | ||
noJwtError: this.noJwtError, | ||
emptyHeaderPrefix: this.noTokenScheme, | ||
globalHeaders: this.globalHeaders | ||
}; | ||
@@ -59,7 +65,17 @@ }; | ||
} | ||
AuthHttp.prototype.setGlobalHeaders = function (headers, request) { | ||
headers.forEach(function (header) { | ||
var key = Object.keys(header)[0]; | ||
var headerValue = header[key]; | ||
request.headers.set(key, headerValue); | ||
}); | ||
}; | ||
AuthHttp.prototype.request = function (url, options) { | ||
var request; | ||
var globalHeaders = this._config.globalHeaders; | ||
if (!tokenNotExpired(null, this._config.tokenGetter())) { | ||
if (!this._config.noJwtError) { | ||
throw 'Invalid JWT'; | ||
return new Observable_1.Observable(function (obs) { | ||
obs.error(new Error('No JWT present')); | ||
}); | ||
} | ||
@@ -75,2 +91,5 @@ else { | ||
} | ||
if (globalHeaders) { | ||
this.setGlobalHeaders(globalHeaders, reqOpts); | ||
} | ||
reqOpts.headers.set(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
@@ -84,2 +103,5 @@ request = this.http.request(url, reqOpts); | ||
} | ||
if (globalHeaders) { | ||
this.setGlobalHeaders(globalHeaders, req); | ||
} | ||
req.headers.set(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
@@ -203,2 +225,10 @@ request = this.http.request(req); | ||
exports.tokenNotExpired = tokenNotExpired; | ||
exports.AUTH_PROVIDERS = [ | ||
core_1.provide(AuthHttp, { | ||
useFactory: function (http) { | ||
return new AuthHttp(new AuthConfig(), http); | ||
}, | ||
deps: [http_1.Http] | ||
}) | ||
]; | ||
//# sourceMappingURL=angular2-jwt.js.map |
@@ -1,2 +0,2 @@ | ||
import {Injectable} from 'angular2/core'; | ||
import {provide, Injectable} from 'angular2/core'; | ||
import {Http, Headers, Request, RequestOptions, RequestOptionsArgs, RequestMethod, Response} from 'angular2/http'; | ||
@@ -14,2 +14,3 @@ import {Observable} from 'rxjs/Observable'; | ||
noJwtError: boolean; | ||
globalHeaders: Array<Object>; | ||
} | ||
@@ -29,2 +30,4 @@ | ||
noJwtError: boolean; | ||
noTokenScheme: boolean; | ||
globalHeaders: Array<Object>; | ||
@@ -34,4 +37,6 @@ constructor(config?: any) { | ||
this.headerName = this.config.headerName || 'Authorization'; | ||
if(this.config.headerPrefix) { | ||
if (this.config.headerPrefix) { | ||
this.headerPrefix = this.config.headerPrefix + ' '; | ||
} else if (this.config.noTokenScheme) { | ||
this.headerPrefix = ''; | ||
} else { | ||
@@ -43,2 +48,3 @@ this.headerPrefix = 'Bearer '; | ||
this.tokenGetter = this.config.tokenGetter || (() => localStorage.getItem(this.tokenName)); | ||
this.globalHeaders = this.config.globalHeaders || null; | ||
} | ||
@@ -52,3 +58,5 @@ | ||
tokenGetter: this.tokenGetter, | ||
noJwtError: this.noJwtError | ||
noJwtError: this.noJwtError, | ||
emptyHeaderPrefix: this.noTokenScheme, | ||
globalHeaders: this.globalHeaders | ||
} | ||
@@ -76,10 +84,21 @@ } | ||
} | ||
setGlobalHeaders(headers: Array<Object>, request: Request | RequestOptionsArgs) { | ||
headers.forEach((header: Object) => { | ||
let key: string = Object.keys(header)[0]; | ||
let headerValue: string = (<any>header)[key]; | ||
request.headers.set(key, headerValue); | ||
}); | ||
} | ||
request(url: string | Request, options?: RequestOptionsArgs) : Observable<Response> { | ||
let request:any; | ||
let request: Observable<Response>; | ||
let globalHeaders = this._config.globalHeaders; | ||
if(!tokenNotExpired(null, this._config.tokenGetter())) { | ||
if(!this._config.noJwtError) { | ||
throw 'Invalid JWT'; | ||
if (!tokenNotExpired(null, this._config.tokenGetter())) { | ||
if (!this._config.noJwtError) { | ||
return new Observable((obs: any) => { | ||
obs.error(new Error('No JWT present')); | ||
}); | ||
} else { | ||
@@ -89,9 +108,13 @@ request = this.http.request(url, options); | ||
} else if(typeof url === 'string') { | ||
let reqOpts = options || {}; | ||
} else if (typeof url === 'string') { | ||
let reqOpts: RequestOptionsArgs = options || {}; | ||
if(!reqOpts.headers) { | ||
if (!reqOpts.headers) { | ||
reqOpts.headers = new Headers(); | ||
} | ||
if (globalHeaders) { | ||
this.setGlobalHeaders(globalHeaders, reqOpts); | ||
} | ||
reqOpts.headers.set(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
@@ -101,8 +124,12 @@ request = this.http.request(url, reqOpts); | ||
} else { | ||
let req:Request = <Request>url; | ||
let req: Request = <Request>url; | ||
if(!req.headers) { | ||
if (!req.headers) { | ||
req.headers = new Headers(); | ||
} | ||
if (globalHeaders) { | ||
this.setGlobalHeaders(globalHeaders, req); | ||
} | ||
req.headers.set(this._config.headerName, this._config.headerPrefix + this._config.tokenGetter()); | ||
@@ -116,5 +143,5 @@ request = this.http.request(req); | ||
private requestHelper(requestArgs: RequestOptionsArgs, additionalOptions: RequestOptionsArgs) : Observable<Response> { | ||
let options = new RequestOptions(requestArgs); | ||
let options: RequestOptions = new RequestOptions(requestArgs); | ||
if(additionalOptions) { | ||
if (additionalOptions) { | ||
options = options.merge(additionalOptions) | ||
@@ -240,1 +267,10 @@ } | ||
} | ||
export const AUTH_PROVIDERS: any = [ | ||
provide(AuthHttp, { | ||
useFactory: (http: Http) => { | ||
return new AuthHttp(new AuthConfig(), http); | ||
}, | ||
deps: [Http] | ||
}) | ||
]; |
{ | ||
"name": "angular2-jwt", | ||
"version": "0.1.11", | ||
"version": "0.1.12", | ||
"description": "Helper library for handling JWTs in Angular 2", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -38,3 +38,3 @@ # angular2-jwt | ||
```ts | ||
import {AuthHttp, AuthConfig} from 'angular2-jwt'; | ||
import {AuthHttp, AuthConfig, AUTH_PROVIDERS} from 'angular2-jwt'; | ||
@@ -61,13 +61,10 @@ ... | ||
HTTP_PROVIDERS, | ||
provide(AuthHttp, { | ||
useFactory: (http) => { | ||
return new AuthHttp(new AuthConfig(), http); | ||
}, | ||
deps: [Http] | ||
}) | ||
AUTH_PROVIDERS | ||
]) | ||
``` | ||
A default configuration for header and token details is provided: | ||
## Configuration Options | ||
`AUTH_PROVIDERS` gives a default configuration setup: | ||
* Header Name: `Authorization` | ||
@@ -78,7 +75,18 @@ * Header Prefix: `Bearer` | ||
* Supress error and continue with regular HTTP request if no JWT is saved: `false` | ||
* Global Headers: none | ||
If you wish to configure the `headerName`, `headerPrefix`, `tokenName`, `tokenGetter` function, or `noJwtError` boolean, you can pass a config object when `AuthHttp` is injected. | ||
If you wish to configure the `headerName`, `headerPrefix`, `tokenName`, `tokenGetter` function, `noTokenScheme`, `globalHeaders`, or `noJwtError` boolean, you can pass a config object when `AuthHttp` is injected. | ||
By default, if there is no valid JWT saved, `AuthHttp` will throw an 'Invalid JWT' error. If you would like to continue with an unauthenticated request instead, you can set `noJwtError` to `true`. | ||
#### Errors | ||
By default, if there is no valid JWT saved, `AuthHttp` will return an Observable `error` with 'Invalid JWT'. If you would like to continue with an unauthenticated request instead, you can set `noJwtError` to `true`. | ||
#### Token Scheme | ||
The default scheme for the `Authorization` header is `Bearer`, but you may either provide your own by specifying a `headerPrefix`, or you may remove the prefix altogether by setting `noTokenScheme` to `true`. | ||
#### Global Headers | ||
You may set as many global headers as you like by passing an array of header-shaped objects to `globalHeaders`. | ||
```ts | ||
@@ -96,3 +104,5 @@ ... | ||
tokenGetter: YOUR_TOKEN_GETTER_FUNCTION, | ||
noJwtError: true | ||
globalHeaders: [{'Content-Type':'application/json'}], | ||
noJwtError: true, | ||
noTokenScheme: true | ||
}), http); | ||
@@ -107,5 +117,5 @@ }, | ||
### Sending Headers | ||
### Sending Per-Request Headers | ||
You may send custom headers with your `authHttp` request by passing in an options object. | ||
You may also send custom headers on a per-request basis with your `authHttp` request by passing them in an options object. | ||
@@ -112,0 +122,0 @@ ```ts |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
85985
1671
250