AngularJS for jwt support accessToken & refreshToken
- HTTP request with Authorization header
- If get 401 response
- Pending all requests
- Fetch refresh token API to get latest access/refresh token
- Fetch all 401 requests sequentially
npm i angularjs-jwt
<script src="https://unpkg.com/angularjs-jwt"></script>
<script>
angular.module('example-app', ['angularjs-jwt'])
.config([
'$httpProvider',
'jwtAuthenticationProvider',
'jwtParcelerProvider',
function (httpProvider, jwtAuthenticationProvider, jwtParcelerProvider) {
jwtAuthenticationProvider.changeOptions({
accessTokenURI: 'yourAccessTokenURI', // Optional
refreshTokenURI: 'yourRefreshTokenURI', // Optional
redirect: 'yourRedirectPage', // Optional
});
// If you want change storeKey, then use below.
jwtParcelerProvider.changeOptions({
storeKeyAccessToken: 'yourStoreKey', // Optional
storeKeyRefreshToken: 'yourStoreKey', // Optional
});
$httpProvider.interceptors.push('jwtInterceptor');
}]);
</script>
import angular from 'angular';
import jwtModule from 'angularjs-jwt';
angular
.module('example-es6', [jwtModule.name])
.config([
'$httpProvider',
'jwtAuthenticationProvider',
'jwtParcelerProvider',
function ($httpProvider, jwtAuthenticationProvider, jwtParcelerProvider) {
jwtAuthenticationProvider.changeOptions({
accessTokenURI: 'yourAccessTokenURI', // Optional
refreshTokenURI: 'yourRefreshTokenURI', // Optional
redirect: 'yourRedirectPage', // Optional
});
// If you want change storeKey, then use below.
jwtParcelerProvider.changeOptions({
storeKeyAccessToken: 'yourStoreKey', // Optional
storeKeyRefreshToken: 'yourStoreKey', // Optional
});
$httpProvider.interceptors.push('jwtInterceptor');
}]);
angular.controller(['jwtAuthentication', function(jwtAuthentication) {
// will setAccessToken & setRefreshToken from data.accessToken/refreshToken
jwtAuthentication.login(username, password);
}]);
angular.controller(['jwtAuthentication', function(jwtAuthentication) {
// Get token...
jwtAuthentication.setAccessToken(yourAccessToken);
jwtAuthentication.setRefreshToken(yourAccessToken);
}]);
MIT © Wilson Weng