Skip to content

Commit

Permalink
Merge pull request #2 from auth0/master
Browse files Browse the repository at this point in the history
catch up
  • Loading branch information
escardin committed Apr 5, 2016
2 parents cbc7e3b + 21a5058 commit c18ea19
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 24 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules
npm-debug.log
typings

Thumbs.db
.DS_Store
Expand Down
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
})
])
```

Expand All @@ -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]
})
])
```

Expand Down Expand Up @@ -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

...
Expand Down
6 changes: 5 additions & 1 deletion angular2-jwt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
14 changes: 8 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
{
"name": "angular2-jwt",
"version": "0.1.4",
"version": "0.1.9",
"description": "Helper library for handling JWTs in Angular 2",
"repository": {
"type": "git",
"url": "git+https://github.com/auth0/angular2-jwt.git"
},
"scripts": {
"dev": "tsc --watch",
"prepublish": "tsc"
"prepublish": "tsc",
"postinstall": "typings install"
},
"keywords": [
"angular",
Expand All @@ -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"
}
}
11 changes: 10 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
]
}
9 changes: 9 additions & 0 deletions typings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"dependencies": {
},
"devDependencies": {
},
"ambientDependencies": {
"es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2"
}
}

0 comments on commit c18ea19

Please sign in to comment.