ducky/web

Paddy 2015-06-30 Parent:99a43a6d1d30 Child:bc1478742a50

12:791aec3eb17b Go to Latest

ducky/web/src/models/profile.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.

History
     1.1 --- a/src/models/profile.js	Tue Jun 30 01:21:42 2015 -0400
     1.2 +++ b/src/models/profile.js	Tue Jun 30 01:23:04 2015 -0400
     1.3 @@ -1,4 +1,5 @@
     1.4  import Model from 'ampersand-model'
     1.5 +import refresh from '../helpers/oauth-refresh'
     1.6  
     1.7  export default Model.extend({
     1.8    props: {
     1.9 @@ -7,4 +8,19 @@
    1.10      'created': 'date',
    1.11      'last_seen': 'date',
    1.12    },
    1.13 +  sync: refresh.Sync,
    1.14 +  parse (attrs) {
    1.15 +    if (attrs.profiles && attrs.profiles.length == 1) {
    1.16 +      attrs = attrs.profiles[0]
    1.17 +    }
    1.18 +    return attrs
    1.19 +  },
    1.20 +  ajaxConfig () {
    1.21 +    return {
    1.22 +      headers: {
    1.23 +        'Authorization': 'Bearer '+app.me.access_token,
    1.24 +        'Accept': 'application/json',
    1.25 +      }
    1.26 +    }
    1.27 +  },
    1.28  })