ducky/web

Paddy 2015-05-03 Child:b9d0efb44eaa

0:99a43a6d1d30 Go to Latest

ducky/web/src/models/me.js

First commit. Setup project structure, start getting our registration flow set up. At this point, it runs successfully locally, assuming the auth server is running locally at slightly.local:8080. So, uh... on my computer. Also, we currently have the Register button (on the register page) disabled always, because we still need to hook up form validation and set the this.state.valid property. If that property is set to true, then the button is enabled again. Still to do: validation, logging in. Then what we have written works, minus some configuration stuff that still needs to be figured out.

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 })