ducky/web
2015-07-07
Parent:d51e39bf909c
ducky/web/src/models/profiles.js
Update to hosted URL, use oauth-refresh in profiles. When syncing the profiles, use our oauth-refresh sync helper, so it won't fail because of an expired OAuth token. Also, update our URL to use the nginx-fronted URL.
1 import Collection from 'ampersand-rest-collection'
2 import Profile from './profile'
3 import config from '../config'
4 import isObject from 'lodash.isobject'
5 import refresh from '../helpers/oauth-refresh'
6 import app from 'ampersand-app'
8 export default Collection.extend({
9 model: Profile,
10 url: config.urlBase + '/auth/profiles',
11 ajaxConfig () {
12 return {
13 headers: {
14 'Authorization': 'Bearer '+app.me.access_token,
15 'Accept': 'application/json',
16 }
17 }
18 },
20 sync: refresh.Sync,
22 register (email, passphrase) {
23 let options = {
24 data: JSON.stringify({
25 'email': email,
26 'passphrase': passphrase,
27 })
28 }
29 let moc = this
30 options.success = function(resp) {
31 if (!resp.profiles || resp.profiles.length < 1) {
32 return false
33 }
34 let serverAttrs = moc.parse(resp.profiles[0], options)
35 if (options.wait) serverAttrs = assign({}, serverAttrs)
36 if (isObject(serverAttrs) && !moc.add(serverAttrs, options)) {
37 return false
38 }
39 moc.trigger('sync', moc, resp, options)
40 }
41 options.error = function(resp) {
42 moc.trigger('error', moc, resp, options)
43 }
44 let sync = refresh.Sync('create', moc, options)
45 return sync
46 },
47 })