Skip to content

Commit

Permalink
Angular 2 beta 0 support
Browse files Browse the repository at this point in the history
  • Loading branch information
chenkie committed Dec 16, 2015
1 parent eb195c1 commit e855ae7
Show file tree
Hide file tree
Showing 12 changed files with 486 additions and 531 deletions.
9 changes: 6 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Node generated files
node_modules
npm-debug.log
# OS generated files

Thumbs.db
.DS_Store
.DS_Store

*.js
*.map
*.d.ts
33 changes: 11 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# angular2-jwt

angular2-jwt is a helper library for working with [JWTs](http://jwt.io/introduction) in your Angular 2 applications.
**angular2-jwt** is a helper library for working with [JWTs](http://jwt.io/introduction) in your Angular 2 applications.

For examples on integrating **angular2-jwt** with Webpack and SystemJS, see [auth0-angular2](https://github.com/auth0/auth0-angular2).

## Key Features

Expand All @@ -24,24 +26,19 @@ The library comes with several helpers that are useful in your Angular 2 apps.

If you wish to only send a JWT on a specific HTTP request, you can use the `AuthHttp` class.

```js
// app.ts

import {Component, View, bootstrap, provide} from 'angular2/angular2';
import {HTTP_PROVIDERS, Http} from 'angular2/http';
import {AuthHttp} from 'angular2-jwt/angular2-jwt';
```ts
import {AuthHttp} from 'angular2-jwt';

...

class App {

thing: string;

constructor(public authHttp:AuthHttp) {}
constructor(public authHttp: AuthHttp) {}

getThing() {
this.authHttp.get('http://example.com/api/thing')
.map(res => res.json())
.subscribe(
data => this.thing = data,
err => console.log(error),
Expand Down Expand Up @@ -70,9 +67,7 @@ If you wish to configure the `headerName`, `headerPrefix`, `tokenName`, `tokenGe

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`.

```js
// app.ts

```ts
...

bootstrap(App, [
Expand All @@ -95,8 +90,8 @@ The `AuthHttp` class supports all the same HTTP verbs as Angular 2's Http.

If you wish to use the JWT as an observable stream, you can call `tokenStream` from `AuthHttp`.

```js
// app.ts
```ts
...

tokenSubscription() {
this.authHttp.tokenStream.subscribe(
Expand All @@ -119,8 +114,7 @@ The `JwtHelper` class has several useful methods that can be utilized in your co

You can use these methods by passing in the token to be evaluated.

```js
// app.ts
```ts

...

Expand All @@ -147,15 +141,10 @@ 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.

```js
// app.ts
```ts

...

import {Component, View, bootstrap, provide} from 'angular2/http';
import {tokenNotExpired} from 'angular2-jwt/angular2-jwt';
import {RouteConfig, RouteParams, ROUTER_DIRECTIVES, APP_BASE_HREF, ROUTER_PROVIDERS, CanActivate} from 'angular2/router'

@Component({
selector: 'secret-route'
})
Expand Down
60 changes: 59 additions & 1 deletion angular2-jwt.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,59 @@
export * from './src/angular2-jwt';
import { Http, RequestMethod, Response } from 'angular2/http';
import { Observable } from 'rxjs/Observable';
export interface IAuthConfig {
headerName: string;
headerPrefix: string;
tokenName: string;
tokenGetter: any;
noJwtError: boolean;
}
/**
* Sets up the authentication configuration.
*/
export declare class AuthConfig {
config: any;
headerName: string;
headerPrefix: string;
tokenName: string;
tokenGetter: any;
noJwtError: boolean;
constructor(config?: any);
getConfig(): {
headerName: string;
headerPrefix: string;
tokenName: string;
tokenGetter: any;
noJwtError: boolean;
};
}
/**
* Allows for explicit authenticated HTTP requests.
*/
export declare class AuthHttp {
private _config;
tokenStream: Observable<string>;
http: Http;
constructor(config?: Object);
request(method: RequestMethod, url: string, body?: string): Observable<Response>;
get(url: string): Observable<Response>;
post(url: string, body: string): Observable<Response>;
put(url: string, body: string): Observable<Response>;
delete(url: string, body?: string): Observable<Response>;
options(url: string, body?: string): Observable<Response>;
head(url: string, body?: string): Observable<Response>;
patch(url: string, body: string): Observable<Response>;
}
/**
* Helper class to decode and find JWT expiration.
*/
export declare class JwtHelper {
urlBase64Decode(str: string): string;
decodeToken(token: string): any;
getTokenExpirationDate(token: string): Date;
isTokenExpired(token: string, offsetSeconds?: number): boolean;
}
/**
* Checks for presence of token and that token hasn't expired.
* For use with the @CanActivate router decorator and NgIf
*/
export declare function tokenNotExpired(tokenName?: string, jwt?: string): boolean;
188 changes: 177 additions & 11 deletions angular2-jwt.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion angular2-jwt.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit e855ae7

Please sign in to comment.