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