Improve this doc View source

TokenStoreManager
service in module stormpath.tokenStore

Description

This service provides methods for registering token stores (with duck-typed validation), as well as retrieving them by name.

Token store implementations must implement the TokenStore interface.

All token stores are expected to satisfy the following contract:

  • Instances must have a put method that takes a key and a value, stores them, and returns a promise indicating success
  • Instances must have a get method that takes a key and returns a promise containing the value for the given key, or a rejection with a reason
  • Instances must have a remove method that takes a key and removes the value, returning the result as a promise

See LocalStorageTokenStore for an example of an implementation.

Methods

Example

angular.module('app')
  .run(['$q', 'TokenStoreManager', function($q, TokenStoreManager) {
    // Can also be provided by a service/factory for better code organisation
    var myStore = {
      data: {},
      get: function get(key) {
        return this.data[key] ? $q.resolve(this.data[key]) : $q.reject();
      },
      put: function put(key, value) {
        this.data[key] = value;
        return $q.resolve();
      },
      remove: function remove(key) {
        delete this.data[key];
        return $q.resolve();
      }
    };

    TokenStoreManager.registerTokenStore('basicStore', myStore);

    var alsoMyStore = TokenStoreManager.getTokenStore('basicStore');
  }]);