angular-authomator

AngularJS module to authenticate against Authomator service

Added by: Sam Deering

GitHub

Repository

authomator/angular-authomator

Stats

Stars: 1

Watchers: 1

Forks: 0

Author

Authomator
Authomator

Module Description

angular-authomator

AngularJS module to authenticate against an Authomator service.

Build Status

  • lightweight (< 4KB minified)
  • no external dependencies
  • automatically handles Authomator query string parameters

Usage

First install the module using bower:

$ bower install angular-authomator

and add the library to your application:

<script type="text/javascript" charset="utf-8" src="bower_components/angular-authomator/dist/angular-authomator.min.js"></script>

Then add the authomator module to the dependencies of your AngularJS application module:

angular.module('yourApp', ['authomator']);

and configure the authomator service in a config block using the provider:

angular
  .module('yourApp')
  .config(function(authomatorProvider){
    authomatorProvider.setOptions({
        authomatorUrl: 'http://your.authomator-service.com'
    });
  });

Now you can use the authomator service anywhere in your application code:

angular
  .module('yourApp')
  .controller('SomeCtrl', function(authomator){
    
    var token = authomator.getAccessToken();
    
    // ...
    
  });

and listen for Authomator events on $rootScope:

angular
  .module('yourApp')
  .controller('SomeCtrl', function($rootScope){
    
    $rootScope.$on('authomator.accessTokenUpdated', function(){
      // ...
    });
    
    $rootScope.$on('authomator.identityTokenUpdated', function(){
      // ...
    });
    
    $rootScope.$on('authomator.refreshTokenUpdated', function(){
      // ...
    });
    
  });

The authomator service

The authomator service can be injected anywhere in your application and exposes the following API:

// Set access token
authomator.setAccessToken(token);

// Get access token
var accessToken = authomator.getAccessToken();

// Remove access token
authomator.removeAccessToken();

// Set identity token
authomator.setIdentityToken(token);

// Get identity token
var identityToken = authomator.getIdentityToken();

// Remove identity token
authomator.removeIdentityToken();

// Set refresh token
authomator.setRefreshToken(token);

// Get refresh token
var refreshToken = authomator.getRefreshToken();

// Remove refresh token
authomator.removeRefreshToken();

The jwtHelpers service

The jwtHelpers service is mainly used internally but can also be injected anywhere in your application and exposes the following API:

// Check if token is expired
jwtHelpers.isTokenExpired(token); // true/false

// Decode a token
var decoded = jwtHelpers.decodeToken(token); // Decoded token

Events

The following events are emitted on $rootScope:

// Listen for an updated access token
$rootScope.$on('authomator.accessTokenUpdated', function(event, decoded){
  // ...
});

// Listen for an updated identity token
$rootScope.$on('authomator.identityTokenUpdated', function(event, decoded){
  // ...
});

// Listen for an updated refresh token
$rootScope.$on('authomator.refreshTokenUpdated', function(event, decoded){
  // ...
});

For optimal performance, events are emitted on $rootScope, not broadcasted, so you have to listen on $rootScope to hear them.

Listening on child $scope instances will not work.

Change log

v0.1.0

  • Added example page
  • Added query string parsing
  • Added authomator service
  • Added jwtHelpers service
  • Added unit tests
  • Added initial documentation

License

Copyright (c) 2015 Jurgen Van de Moere

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Module stats last updated: 2015-04-15 19:53:36

Disclaimer: Some data on this page may have been gathered from the authors GitHub respository. If you see any mistakes or outdated information please let us know. Thanks!