ducky/web
ducky/web/src/models/profiles.js
Update our profile model to use our refresh helper. Update our profile model to send the correct authorization header when making requests, and if the request fails because the OAuth token has expired, try to use the refresh token to obtain a new access token, then retry the request.
| paddy@0 | 1 import Collection from 'ampersand-collection' |
| paddy@0 | 2 import Sync from 'ampersand-sync' |
| paddy@0 | 3 import Profile from './profile' |
| paddy@0 | 4 import config from '../config' |
| paddy@0 | 5 import isObject from 'lodash.isobject' |
| paddy@0 | 6 |
| paddy@0 | 7 export default Collection.extend({ |
| paddy@0 | 8 model: Profile, |
| paddy@0 | 9 url: config.urlBase + '/profiles', |
| paddy@0 | 10 ajaxConfig: { |
| paddy@0 | 11 headers: { |
| paddy@0 | 12 'Content-Type': 'application/json', |
| paddy@0 | 13 } |
| paddy@0 | 14 }, |
| paddy@0 | 15 |
| paddy@0 | 16 register (email, passphrase) { |
| paddy@0 | 17 let options = { |
| paddy@0 | 18 data: JSON.stringify({ |
| paddy@0 | 19 'email': email, |
| paddy@0 | 20 'passphrase': passphrase, |
| paddy@0 | 21 }) |
| paddy@0 | 22 } |
| paddy@0 | 23 let moc = this |
| paddy@0 | 24 options.success = function(resp) { |
| paddy@0 | 25 if (!resp.profiles || resp.profiles.length < 1) { |
| paddy@0 | 26 return false |
| paddy@0 | 27 } |
| paddy@0 | 28 let serverAttrs = moc.parse(resp.profiles[0], options) |
| paddy@0 | 29 if (options.wait) serverAttrs = assign({}, serverAttrs) |
| paddy@0 | 30 if (isObject(serverAttrs) && !moc.add(serverAttrs, options)) { |
| paddy@0 | 31 return false |
| paddy@0 | 32 } |
| paddy@0 | 33 moc.trigger('sync', moc, resp, options) |
| paddy@0 | 34 } |
| paddy@0 | 35 options.error = function(resp) { |
| paddy@0 | 36 moc.trigger('error', moc, resp, options) |
| paddy@0 | 37 } |
| paddy@0 | 38 let sync = Sync('create', moc, options) |
| paddy@0 | 39 return sync |
| paddy@0 | 40 }, |
| paddy@0 | 41 }) |