Persist session data to localStorage.
Create a helper library that figures out whether to write to
chrome.storage.local or window.localStorage, and unifies their two APIs.
Update the Me model to use the getOrFetch method for the profiles collection
when retrieving the user's profile. This, unfortunately, makes it an async call
(because we may need to fetch data from the server), so we can no longer have it
be a derived property, which is a shame. It instead must just be the me.profile()
function.
Separate out the logic to determine when an access token expires, and turn it
into the tokenExpires function.
Fill the writeToCache placeholder with the logic to store the current session in
either window.localStorage or chrome.storage.local, whichever is the more
appropriate, using the helper library.
Create the load helper function that will attempt to read session data from
localStorage or chrome.storage.local, whichever the library decides is
available, and updates the session based on it.
Implement the logout function, which just uses the helper library to remove the
session data from window.localStorage or chrome.storage.local. We should also be
resetting the app.me variable, however.
Create a debouncedWriteToCache function that will only write to the cache once
every 250 ms, to avoid rushes on the cache.
When instantiating our app.me variable, load it in from localStorage or
chrome.storage.local if we can. Also, listen for changes to app.me, and persist
them to chrome.storage.local or localStorage.
1 import Model from 'ampersand-model'
2 import Sync from 'ampersand-sync'
4 import config from '../config'
5 import isObject from 'lodash.isobject'
6 import jwtDecode from 'jwt-decode'
7 import localStore from '../helpers/local-storage'
8 import debounce from 'lodash.debounce'
10 export default Model.extend({
11 url: config.urlBase + '/token',
14 'Content-Type': 'application/x-www-form-urlencoded',
15 'Authorization': 'Basic ' + btoa(config.clientID + ':' + config.clientSecret),
20 access_token: 'string',
21 refresh_token: 'string',
23 token_created: 'date',
28 return new Promise((resolve, reject) => {
29 app.profiles.getOrFetch(this.profileID, (err, model) => {
41 return !!this.access_token
44 let d = this.token_created
45 d.setSeconds(d.getSeconds() + this.expires_in)
49 return !!this.refresh_token && (new Date() >= this.tokenExpires)
54 this.debouncedWriteToCache = debounce(this.writeToCache, 250)
57 login (email, password) {
62 'grant_type': 'password',
66 options.success = function(resp) {
67 if (!resp.access_token) {
70 let serverAttrs = moc.parse(resp, options)
71 serverAttrs.token_created = new Date()
72 if (options.wait) serverAttrs = assign({}, serverAttrs)
73 if (isObject(serverAttrs) && !moc.set(serverAttrs, options)) {
76 const token = jwtDecode(moc.access_token)
77 moc.profileID = token.sub
78 moc.trigger('sync', moc, resp, options)
80 options.error = function(resp) {
81 moc.trigger('error', moc, resp, options)
83 let sync = Sync('create', moc, options)
87 const data = JSON.stringify(this)
88 localStore.set('me', data)
93 localStore.get('me').catch((err) => {
97 const loaded = this.parse(JSON.parse(resp))
98 moc.set(loaded, {silent: true})
105 localStore.remove('me')