Skip to content

Commit

Permalink
Merge pull request #1 from gdi2290/patch-1
Browse files Browse the repository at this point in the history
docs(readme): update examples for 1.3+
  • Loading branch information
mgonto committed Oct 1, 2014
2 parents 86df8a9 + 0d2d999 commit f98a70e
Showing 1 changed file with 18 additions and 15 deletions.
33 changes: 18 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,26 +25,29 @@ jwtHelper will take care of helping you decode the token and check its expiratio
### Decoding the token

````js
function Controller(jwtHelper) {
angular.module('app', [])
.controller('Controller', function Controller(jwtHelper) {
var expToken = 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwczovL3NhbXBsZXMuYXV0aDAuY29tLyIsInN1YiI6ImZhY2Vib29rfDEwMTU0Mjg3MDI3NTEwMzAyIiwiYXVkIjoiQlVJSlNXOXg2MHNJSEJ3OEtkOUVtQ2JqOGVESUZ4REMiLCJleHAiOjE0MTIyMzQ3MzAsImlhdCI6MTQxMjE5ODczMH0.7M5sAV50fF1-_h9qVbdSgqAnXVF7mz3I6RjS6JiH0H8';

var tokenPayload = jwtHelper.decodeToken(expToken);
}
})
````
### Getting the token expiration date

````js
function Controller(jwtHelper) {
angular.module('app', [])
.controller('Controller', function Controller(jwtHelper) {
var date = jwtHelper.getTokenExpirationDate(expToken);
}
})
````

### Checking if token is expired

````js
function Controller(jwtHelper) {
angular.module('app', [])
.controller('Controller', function Controller(jwtHelper) {
var bool = jwtHelper.isTokenExpired(expToken);
}
})
````

### More examples
Expand All @@ -58,14 +61,14 @@ JWT interceptor will take care of sending the JWT in every request.
### Basic usage

````js
function Config($httpProvider, jwtInterceptorProvider) {
angular.module('app', [])
.config(function Config($httpProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.tokenGetter = function(localStorage) {
return localStorage.getItem('id_token');
}
$httpProvider.interceptors.push('jwtInterceptor');
}

function Controller($http) {
})
.controller('Controller', function Controller($http) {
// If localStorage contains the id_token it will be sent in the request
// Authorization: Bearer [yourToken] will be sent
$http({
Expand All @@ -80,7 +83,8 @@ function Controller($http) {
As sometimes we need to get first the `id_token` in order to send it, we can return a promise in the `tokenGetter`. Let's see for example how we'd use a `refresh_token`

````js
function Config($httpProvider, jwtInterceptorProvider) {
angular.module('app', [])
.config(function Config($httpProvider, jwtInterceptorProvider) {
jwtInterceptorProvider.tokenGetter = function(localStorage, jwtHelper) {
var idToken = localStorage.getItem('id_token');
var refreshToken = localStorage.getItem('refresh_token');
Expand All @@ -95,16 +99,15 @@ function Config($httpProvider, jwtInterceptorProvider) {
}
}
$httpProvider.interceptors.push('jwtInterceptor');
}

function Controller($http) {
})
.controller('Controller', function Controller($http) {
// Authorization: Bearer [yourToken] will be sent.
// That token might be a new one which was got from the refresh token
$http({
url: '/hola',
method: 'GET'
});
}
})
````

### More examples
Expand Down

0 comments on commit f98a70e

Please sign in to comment.