Skip to content

Commit 7edc577

Browse files
committed
Add support for storageService objects
Implements #11 to add support for the storageService configuration value to be an object that supports the expected storage API.
1 parent c48ceec commit 7edc577

File tree

4 files changed

+48
-9
lines changed

4 files changed

+48
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
node_modules/
22
*.log
33
temp/
4+
package-lock.json

README.md

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ All properties (own and inherited) of the extensions object will be available as
7676

7777
If you do not provide a storage service then a simple, in-memory dictionary will be used.
7878

79-
You can provide any storage service that supports the following API:
79+
You can provide any storage service or object that supports the following API:
8080

81-
1. ```mixed get(key)```
81+
1. ```any get(key)```
8282
2. ```boolean has(key)```
8383
3. ```void remove(key)```
8484
4. ```void set(key, value)```
@@ -93,6 +93,16 @@ app.config(['$authenticationProvider', function ($authenticationProvider) {
9393
}]);
9494
```
9595

96+
or an object that provides the expected functionality:
97+
98+
```JAVASCRIPT
99+
app.config(['$authenticationProvider', function ($authenticationProvider) {
100+
$authenticationProvider.configure({
101+
storageService: new CustomStorageService()
102+
});
103+
}]);
104+
```
105+
96106
The ng-authentication-service was designed in tandem with the [ng-local-storage-service](https://github.com/justinsa/angular-local-storage-service).
97107

98108
## API

ng-authentication-service.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
var storeService;
6464
var storageService = function () {
6565
if (storeService === undefined) {
66-
if (!_.isString(configuration.storageService)) {
66+
if (_.isNil(configuration.storageService)) {
6767
// Use a simple, in-memory storage option
6868
storeService = {
6969
dictionary: {},
@@ -82,12 +82,18 @@
8282
}
8383
};
8484
return storeService;
85+
} else if (_.isString(configuration.storageService)) {
86+
// Get the service from the $injector
87+
if (!$injector.has(configuration.storageService)) {
88+
$log.error('No matching service registered in Angular: ', configuration.storageService);
89+
return undefined;
90+
}
91+
storeService = $injector.get(configuration.storageService);
92+
} else {
93+
// The configuration object is the service
94+
storeService = configuration.storageService;
8595
}
86-
if (!$injector.has(configuration.storageService)) {
87-
$log.error('No matching service registered in Angular: ', configuration.storageService);
88-
return undefined;
89-
}
90-
storeService = $injector.get(configuration.storageService);
96+
9197
_.each(['get', 'has', 'remove', 'set'], function (fnName) {
9298
if (!_.hasIn(storeService, fnName) || !_.isFunction(storeService[fnName])) {
9399
$log.error('storageService is missing function: ', fnName);

tests.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,28 @@ describe('$authentication', function () {
6767
})
6868
);
6969

70+
describe('with object storageService', function () {
71+
var storage = {
72+
get: function () {},
73+
has: function () {},
74+
remove: function () {},
75+
set: function () {},
76+
test: function () { return 'test'; }
77+
};
78+
79+
beforeEach(
80+
module('authentication.service', function ($authenticationProvider) {
81+
$authenticationProvider.configure({
82+
storageService: storage
83+
});
84+
})
85+
);
86+
87+
it('should use the provided storage object', inject(function ($authentication) {
88+
$authentication.store().test().should.equal('test');
89+
}));
90+
});
91+
7092
describe('with extensions', function () {
7193
beforeEach(function () {
7294
module('authentication.service', function ($authenticationProvider) {
@@ -82,7 +104,7 @@ describe('$authentication', function () {
82104
});
83105
});
84106

85-
it('should expose the provided fields that do not conflict with the existing api',
107+
it('should expose the provided fields that do not conflict with the existing API',
86108
inject(function ($authentication) {
87109
$authentication.key1.should.equal('key1');
88110
$authentication.key2.should.be.a.Function();

0 commit comments

Comments
 (0)