Skip to content

Commit

Permalink
Using regular localStorage instead of localforage to avoid async comp…
Browse files Browse the repository at this point in the history
…lications
  • Loading branch information
DavidMikeSimon committed Feb 15, 2017
1 parent 9c1e17e commit 4da08c7
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 67 deletions.
3 changes: 1 addition & 2 deletions .jshintrc
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
"sub": true,
"globals": {
"angular": false,
"jwt_decode": false,
"localforage": false
"jwt_decode": false
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ a POST request with a JSON body like so:
```js
{
"auth": {
"email": "joe",
"email": "joe@domain.com",
"password": "coffee"
}
}
Expand Down
3 changes: 1 addition & 2 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
"dependencies": {
"angular": "1.5.*",
"angular-resource": "1.5.*",
"jwt-decode": "2.1.0",
"localforage": "1.4.3"
"jwt-decode": "2.1.0"
},
"devDependencies": {
"angular-mocks": "1.5.*",
Expand Down
41 changes: 30 additions & 11 deletions build/secure-ng-resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* secure-ng-resource JavaScript Library
* https://github.com/AmericanCouncils/secure-ng-resource/
* License: MIT (http://www.opensource.org/licenses/mit-license.php)
* Compiled At: 02/14/2017 14:49
* Compiled At: 02/15/2017 11:19
***********************************************/
(function(window) {
'use strict';
Expand All @@ -14,8 +14,8 @@ angular.module('secureNgResource', [

angular.module('secureNgResource')
.factory('authSession', [
'$q', '$location', '$injector', '$rootScope', '$timeout',
function($q, $location, $injector, $rootScope, $timeout) {
'$q', '$location', '$injector', '$rootScope', '$timeout', '$window',
function($q, $location, $injector, $rootScope, $timeout, $window) {
var DEFAULT_SETTINGS = {
sessionName: 'angular',
loginPath: '/login',
Expand All @@ -36,22 +36,28 @@ function($q, $location, $injector, $rootScope, $timeout) {
this.state = {};
this.managedHttpConfs = [];
this.refreshPromise = null;
this.ready = false;

sessionDictionary[this.storageKey()] = this;

var me = this;
localforage.getItem(this.storageKey())
.then(function(storedState) {
if (this._canUseLocalStorage()) {
var storedState = $window.localStorage.getItem(this.storageKey());
if (storedState) {
me.state = storedState;
me._onStateChange();
this.state = JSON.parse(storedState);
this._onStateChange();
} else {
me.reset();
this.reset();
}
});
} else {
this.reset();
}
}

AuthSession.prototype = {
isReady: function () {
return this.ready;
},

getUserName: function () {
if (this.loggedIn()) {
return this.state.user;
Expand Down Expand Up @@ -204,7 +210,9 @@ function($q, $location, $injector, $rootScope, $timeout) {
_onStateChange: function() {
this.reupdateManagedRequestConfs();

localforage.setItem(this.storageKey(), this.state);
if (this._canUseLocalStorage) {
$window.localStorage.setItem(this.storageKey(), JSON.stringify(this.state));
}

if (this.refreshPromise !== null) {
$timeout.cancel(this.refreshPromise);
Expand All @@ -218,6 +226,17 @@ function($q, $location, $injector, $rootScope, $timeout) {
this.state.millisecondsToRefresh
);
}
},

_canUseLocalStorage: function() {
try {
var x = '__storage_test__';
$window.localStorage.setItem(x, x);
$window.localStorage.removeItem(x);
return true;
} catch(e) {
return false;
}
}
};

Expand Down
2 changes: 1 addition & 1 deletion build/secure-ng-resource.min.js

Large diffs are not rendered by default.

34 changes: 24 additions & 10 deletions src/services/AuthSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

angular.module('secureNgResource')
.factory('authSession', [
'$q', '$location', '$injector', '$rootScope', '$timeout',
function($q, $location, $injector, $rootScope, $timeout) {
'$q', '$location', '$injector', '$rootScope', '$timeout', '$window',
function($q, $location, $injector, $rootScope, $timeout, $window) {
var DEFAULT_SETTINGS = {
sessionName: 'angular',
loginPath: '/login',
Expand All @@ -27,16 +27,17 @@ function($q, $location, $injector, $rootScope, $timeout) {

sessionDictionary[this.storageKey()] = this;

var me = this;
localforage.getItem(this.storageKey())
.then(function(storedState) {
if (this._canUseLocalStorage()) {
var storedState = $window.localStorage.getItem(this.storageKey());
if (storedState) {
me.state = storedState;
me._onStateChange();
this.state = JSON.parse(storedState);
this._onStateChange();
} else {
me.reset();
this.reset();
}
});
} else {
this.reset();
}
}

AuthSession.prototype = {
Expand Down Expand Up @@ -204,7 +205,9 @@ function($q, $location, $injector, $rootScope, $timeout) {
_onStateChange: function() {
this.reupdateManagedRequestConfs();

localforage.setItem(this.storageKey(), this.state);
if (this._canUseLocalStorage) {
$window.localStorage.setItem(this.storageKey(), JSON.stringify(this.state));
}

if (this.refreshPromise !== null) {
$timeout.cancel(this.refreshPromise);
Expand All @@ -218,6 +221,17 @@ function($q, $location, $injector, $rootScope, $timeout) {
this.state.millisecondsToRefresh
);
}
},

_canUseLocalStorage: function() {
try {
var x = '__storage_test__';
$window.localStorage.setItem(x, x);
$window.localStorage.removeItem(x);
return true;
} catch(e) {
return false;
}
}
};

Expand Down
1 change: 0 additions & 1 deletion test/karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ module.exports = function(config) {
'bower_components/angular/angular.js',
'bower_components/angular-resource/angular-resource.js',
'bower_components/angular-mocks/angular-mocks.js',
'bower_components/localforage/dist/localforage.js',
'build/secure-ng-resource.debug.js',
'test/unit/*Spec.js'
],
Expand Down
53 changes: 14 additions & 39 deletions test/unit/AuthSessionSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ describe('AuthSession', function () {
afterEach(function() {
$httpBackend.verifyNoOutstandingExpectation();
$httpBackend.verifyNoOutstandingRequest();
localforage.removeItem('foo-spyAuth');
localforage.removeItem('angular-spyAuth');
window.localStorage.removeItem('foo-spyAuth');
window.localStorage.removeItem('angular-spyAuth');
});

it('has the correct initial state by default', function() {
Expand All @@ -73,45 +73,20 @@ describe('AuthSession', function () {
expect(ses2.storageKey()).toEqual('foo-spyAuth');
});

it('caches and retrieves session state with localforage', function () {
var r1, r2;
var ready = false;

runs(function() {
localforage.getItem('foo-spyAuth')
.then(function (result) {
r1 = result;
var ses2 = sessionFactory(auth, {sessionName: 'foo'});
ses2.login({});
$scope.$apply();
return localforage.getItem('foo-spyAuth');
}).then(function (ses2storage) {
r2 = ses2storage;
ready = true;
})
});

waitsFor(function() { return ready; });

runs(function() {
expect(r1).toBeNull();
expect(r2.user).toEqual('someone');
});
it('caches and retrieves session state with local storage', function () {
expect(window.localStorage.getItem('foo-spyAuth')).toBeNull();
var ses2 = sessionFactory(auth, {sessionName: 'foo'});
ses2.login({});
$scope.$apply();
var stored = JSON.parse(window.localStorage.getItem('foo-spyAuth'));
expect(stored.user).toEqual('someone');
});

it('loads state from localforage by default', function () {
var r;
var ready = false;

runs(function() {
localforage.setItem('foo-spyAuth', {user: 'alice'})
.then(function (result) {
r = sessionFactory(auth, {sessionName: 'foo'});
ready = true;
})
});

waitsFor(function() { return ready && r.loggedIn(); });
it('loads state from local storage by default', function () {
window.localStorage.setItem('foo-spyAuth', JSON.stringify({user: 'alice'}))
var ses = sessionFactory(auth, {sessionName: 'foo'});
expect(ses.loggedIn()).toBeTruthy();
expect(ses.getUserName()).toEqual('alice');
});

it('accepts logins which the authenticator approves', function() {
Expand Down

0 comments on commit 4da08c7

Please sign in to comment.