Angular-JWT
Angulat-jwt est une librairie qui vous aide à gérer les jetons JWT (JSON WEB TOKEN) dans votre application Angular. Combiné à un intercepteur, il vous permet de joindre le jeton à toutes vos requêtes Ajax automatiquement.
Installation
$ bower install angular-jwt --save
.module('myApp', [
'ngAnimate',
...
'angular-jwt'
])
Configuration
angular.module('myApp').config(function ($locationProvider, $httpProvider, jwtInterceptorProvider)
{
jwtInterceptorProvider.tokenGetter = function(config, jwtHelper) {
// Do not use token to get .html templates
if (config.url.substr(config.url.length - 5) === '.html' ||
config.url.indexOf('/api/') === -1
)
{
return null;
}
var jwt = localStorage.getItem('JWT');
if(jwt === null)
{
return null;
}
if (jwtHelper.isTokenExpired(jwt))
{
console.log("Token Expired !", jwtHelper.getTokenExpirationDate(jwt));
}
else
{
console.log("Token not expired", jwtHelper.getTokenExpirationDate(jwt));
return jwt;
}
};
$httpProvider.interceptors.push('jwtInterceptor');
}) ;










Laisser un commentaire
Rejoindre la discussion?N’hésitez pas à contribuer !