Skip to content

Library to help you work with JWT accessToken/refreshToken on AngularJS

License

Notifications You must be signed in to change notification settings

l7960261/angularjs-jwt

Repository files navigation

angularjs-jwt

Build Status dependencies Status devDependencies Status License: MIT

Features

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

Install

npm i angularjs-jwt

Use in HTML

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

Use in es6

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');
  }]);

jwtAuthentication.login

angular.controller(['jwtAuthentication', function(jwtAuthentication) {
  // will setAccessToken & setRefreshToken from data.accessToken/refreshToken
  jwtAuthentication.login(username, password);
}]);

jwtAuthentication.setAccessToken/RefreshToken

angular.controller(['jwtAuthentication', function(jwtAuthentication) {
  // Get token...
  jwtAuthentication.setAccessToken(yourAccessToken);
  jwtAuthentication.setRefreshToken(yourAccessToken);
}]);

License

MIT © Wilson Weng

About

Library to help you work with JWT accessToken/refreshToken on AngularJS

Resources

License

Stars

Watchers

Forks

Packages

No packages published