-
Notifications
You must be signed in to change notification settings - Fork 602
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add SessionStorage based store #356
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
packages/ember-simple-auth/lib/simple-auth/stores/session-storage.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import Base from './base'; | ||
import objectsAreEqual from '../utils/objects-are-equal'; | ||
import Configuration from '../configuration'; | ||
|
||
/** | ||
Store that saves its data in the browser's `sessionStorage`. | ||
|
||
_The factory for this store is registered as | ||
`'simple-auth-session-store:session-storage'` in Ember's container._ | ||
|
||
This store behaves much like the localStorage store, except that it uses the sessionStorage object, | ||
This means it will not persist beyond the life of the browser session | ||
|
||
@class SessionStorage | ||
@namespace SimpleAuth.Stores | ||
@module simple-auth/stores/session-storage | ||
@extends Stores.Base | ||
*/ | ||
export default Base.extend({ | ||
/** | ||
The key the store stores the data in. | ||
|
||
@property key | ||
@type String | ||
@default 'ember_simple_auth:session' | ||
*/ | ||
key: 'ember_simple_auth:session', | ||
|
||
/** | ||
@method init | ||
@private | ||
*/ | ||
init: function() { | ||
this.key = Configuration.sessionStorageKey; | ||
|
||
this.bindToStorageEvents(); | ||
}, | ||
|
||
/** | ||
Persists the `data` in the `sessionStorage`. | ||
|
||
@method persist | ||
@param {Object} data The data to persist | ||
*/ | ||
persist: function(data) { | ||
data = JSON.stringify(data || {}); | ||
sessionStorage.setItem(this.key, data); | ||
this._lastData = this.restore(); | ||
}, | ||
|
||
/** | ||
Restores all data currently saved in the `sessionStorage` identified by the | ||
`keyPrefix` as one plain object. | ||
|
||
@method restore | ||
@return {Object} All data currently persisted in the `sessionStorage` | ||
*/ | ||
restore: function() { | ||
var data = sessionStorage.getItem(this.key); | ||
return JSON.parse(data) || {}; | ||
}, | ||
|
||
/** | ||
Clears the store by deleting all `sessionStorage` keys prefixed with the | ||
`keyPrefix`. | ||
|
||
@method clear | ||
*/ | ||
clear: function() { | ||
sessionStorage.removeItem(this.key); | ||
this._lastData = {}; | ||
}, | ||
|
||
/** | ||
@method bindToStorageEvents | ||
@private | ||
*/ | ||
bindToStorageEvents: function() { | ||
var _this = this; | ||
Ember.$(window).bind('storage', function(e) { | ||
var data = _this.restore(); | ||
if (!objectsAreEqual(data, _this._lastData)) { | ||
_this._lastData = data; | ||
_this.trigger('sessionDataUpdated', data); | ||
} | ||
}); | ||
} | ||
}); |
24 changes: 24 additions & 0 deletions
24
packages/ember-simple-auth/tests/simple-auth/stores/session-storage-test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import SessionStorage from 'simple-auth/stores/session-storage'; | ||
import Configuration from 'simple-auth/configuration'; | ||
import itBehavesLikeAStore from './shared/store-behavior'; | ||
|
||
describe('Stores.SessionStorage', function() { | ||
beforeEach(function() { | ||
this.store = SessionStorage.create(); | ||
this.store.clear(); | ||
}); | ||
|
||
itBehavesLikeAStore(); | ||
|
||
describe('initilization', function() { | ||
it('defaults key to "ember_simple_auth:session"', function() { | ||
expect(SessionStorage.create().key).to.eq('ember_simple_auth:session'); | ||
}); | ||
|
||
it('assigns sessionStorageKey from the configuration object', function() { | ||
Configuration.sessionStorageKey = 'sessionStorageKey'; | ||
|
||
expect(SessionStorage.create().key).to.eq('sessionStorageKey'); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This shouldn't be necessary as sessionStorage is isolated between tabs/windows anyway.