ducky/web

Paddy 2015-05-03 Parent:99a43a6d1d30 Child:b9d0efb44eaa

1:2cd4e16a669e Go to Latest

ducky/web/src/models/me.js

Remove .swp files, update package.json. Remove the .swp files and add .swp to the .hgignore file. Update package.json to pin webpack at 1.8.9 because 1.8.10 broke everything and decided that didn't deserve a major version bump. See https://github.com/webpack/webpack/issues/1016

History
1 import Model from 'ampersand-model'
2 import Sync from 'ampersand-sync'
3 import qs from 'qs'
4 import config from '../config'
5 import isObject from 'lodash.isobject'
7 export default Model.extend({
8 url: config.urlBase + '/token',
9 ajaxConfig: {
10 headers: {
11 'Content-Type': 'application/x-www-form-urlencoded',
12 'Authorization': 'Basic ' + btoa(config.clientID + ':' + config.clientSecret),
13 }
14 },
16 props: {
17 access_token: 'string',
18 refresh_token: 'string',
19 expires_in: 'int',
20 token_created: 'date',
21 name: 'string',
22 },
24 derived: {
25 loggedIn () {
26 return !!this.access_token
27 },
28 needsRefresh () {
29 let d = this.token_created
30 return !!this.refresh_token && (new Date() >= d.setSeconds(d.getSeconds() + this.expires_in - 900))
31 },
32 },
34 login (email, password) {
35 let options = {
36 data: qs.stringify({
37 'username': email,
38 'password': password,
39 'grant_type': 'password',
40 }),
41 }
42 let moc = this
43 options.success = function(resp) {
44 if (!resp.access_token) {
45 return false
46 }
47 let serverAttrs = moc.parse(resp, options)
48 serverAttrs.token_created = new Date()
49 console.log(serverAttrs)
50 if (options.wait) serverAttrs = assign({}, serverAttrs)
51 if (isObject(serverAttrs) && !moc.set(serverAttrs, options)) {
52 return false
53 }
54 moc.trigger('sync', moc, resp, options)
55 }
56 options.error = function(resp) {
57 moc.trigger('error', moc, resp, options)
58 }
59 let sync = Sync('create', moc, options)
60 },
62 writeToCache () {
63 // TODO: write this to chrome.storage.local
64 },
66 logout () {
67 // TODO: clear all cached data
68 },
70 })