diff --git a/.gitignore b/.gitignore index 09f0e659..bab3d3e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ node_modules npm-debug.log +typings Thumbs.db .DS_Store diff --git a/README.md b/README.md index ddb16c7b..acaf06a3 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,14 @@ For examples on integrating **angular2-jwt** with Webpack and SystemJS, see [auth0-angular2](https://github.com/auth0/auth0-angular2). +## What is This Library For? + +**angular2-jwt** is a small and unopinionated library that is useful for automatically attaching a [JSON Web Token (JWT)](http://jwt.io/introduction) as an `Authorization` header when making HTTP requests from an Angular 2 app. It also has a number of helper methods that are useful for doing things like decoding JWTs. + +This library does not have any functionality or opinion about how you should be implementing user authentication and retrieving JWTs to begin with. Those details will vary depending on your setup, but in most cases, you will use a regular HTTP request to authenticate your users and then save their JWTs in local storage or in a cookie if successful. + +For more on implementing authentication endpoints, see this tutorial for an [example using HapiJS](https://auth0.com/blog/2016/03/07/hapijs-authentication-secure-your-api-with-json-web-tokens/). + ## Key Features * Send a JWT on a per-request basis using the **explicit `AuthHttp`** class @@ -49,12 +57,12 @@ class App { bootstrap(App, [ HTTP_PROVIDERS, - provide(AuthConfig, { - useFactory: () => { - return new AuthConfig(); - } - }), - AuthHttp + provide(AuthHttp, { + useFactory: (http) => { + return new AuthHttp(new AuthConfig(), http); + }, + deps: [Http] + }) ]) ``` @@ -75,16 +83,18 @@ By default, if there is no valid JWT saved, `AuthHttp` will throw an 'Invalid JW bootstrap(App, [ HTTP_PROVIDERS, - provide(AuthConfig, { useFactory: () => { - return new AuthConfig({ - headerName: YOUR_HEADER_NAME, - headerPrefix: YOUR_HEADER_PREFIX, - tokenName: YOUR_TOKEN_NAME, - tokenGetter: YOUR_TOKEN_GETTER_FUNCTION, - noJwtError: true - }) - }}), - AuthHttp + provide(AuthHttp, { + useFactory: (http) => { + return new AuthHttp(new AuthConfig({ + headerName: YOUR_HEADER_NAME, + headerPrefix: YOUR_HEADER_PREFIX, + tokenName: YOUR_TOKEN_NAME, + tokenGetter: YOUR_TOKEN_GETTER_FUNCTION, + noJwtError: true + }), http); + }, + deps: [Http] + }) ]) ``` @@ -171,6 +181,8 @@ The `tokenNotExpired` function can be used to check whether a JWT exists in loca The router's `@CanActivate` lifecycle hook can be used with `tokenNotExpired` to determine if a route should be accessible. This lifecycle hook is run before the component class instantiates. If `@CanActivate` receives `true`, the router will allow navigation, and if it receives `false`, it won't. +> **Note:** `tokenNotExpired` will by default assume the token name is `id_token` unless a token name is passed to it, ex: `tokenNotExpired('token_name')`. This will be changed in a future release to automatically use the token name that is set in `AuthConfig`. + ```ts ... diff --git a/angular2-jwt.ts b/angular2-jwt.ts index f1c7476f..7524c9fa 100644 --- a/angular2-jwt.ts +++ b/angular2-jwt.ts @@ -29,7 +29,11 @@ export class AuthConfig { constructor(config?: any) { this.config = config || {}; this.headerName = this.config.headerName || 'Authorization'; - this.headerPrefix = this.config.headerPrefix + ' ' || 'Bearer '; + if(this.config.headerPrefix) { + this.headerPrefix = this.config.headerPrefix + ' '; + } else { + this.headerPrefix = 'Bearer '; + } this.tokenName = this.config.tokenName || 'id_token'; this.noJwtError = this.config.noJwtError || false; this.tokenGetter = this.config.tokenGetter || (() => localStorage.getItem(this.tokenName)); diff --git a/package.json b/package.json index 8524dec0..4f4b16ed 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "angular2-jwt", - "version": "0.1.4", + "version": "0.1.9", "description": "Helper library for handling JWTs in Angular 2", "repository": { "type": "git", @@ -8,7 +8,8 @@ }, "scripts": { "dev": "tsc --watch", - "prepublish": "tsc" + "prepublish": "tsc", + "postinstall": "typings install" }, "keywords": [ "angular", @@ -25,12 +26,13 @@ "typings": "./angular2-jwt.d.ts", "homepage": "https://github.com/auth0/angular2-jwt#readme", "dependencies": { - "angular2": "^2.0.0-beta.0", - "zone.js": "^0.5.10", - "rxjs": "^5.0.0-beta.0" + "angular2": ">=2.0.0-beta.12", + "zone.js": "^0.6.6", + "rxjs": "5.0.0-beta.2", + "typings": "^0.7.9" }, "devDependencies": { "systemjs": "~0.19.6", - "typescript": "~1.7.3" + "typescript": "^1.8.9" } } diff --git a/tsconfig.json b/tsconfig.json index aa38610a..13801010 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -8,5 +8,14 @@ "sourceMap": true, "declaration": true }, - "exclude": ["node_modules"] + "exclude": [ + "node_modules", + "typings/main.d.ts", + "typings/main" + ], + "filesGlob": [ + "*.ts", + "!./node_modules/**/*.ts", + "typings/browser.d.ts" + ] } \ No newline at end of file diff --git a/typings.json b/typings.json new file mode 100644 index 00000000..21c6a017 --- /dev/null +++ b/typings.json @@ -0,0 +1,9 @@ +{ + "dependencies": { + }, + "devDependencies": { + }, + "ambientDependencies": { + "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2" + } +}