ducky/web
6:a641906b8267
Go to Latest
ducky/web/src/models/me.js
Enable catch-all in our ValidationError component.
We're doing this an ugly, hacky way. But it works, and right now, that's what
counts.
To match our params/fields/headers properties on the ValidationError component,
we're going to add the notParams/notFields/notHeaders properties--they match any
error _not_ targeting those params/fields/headers. Basically, "any error that
wouldn't be caught by these filters". Which is an ugly, but workable, solution
for a catch-all ValidationError--just tell it to catch anything but the
params/fields/headers that are being handled by the other ValidationErrors.
Our implementation of this in the RegisterPage component validates (ha!) that
it's at least workable model, if not overly pretty. Also, I anticipate some
human error bugs in the future, where one of the field-specific ValidationErrors
gets updated and the catch-all ValidationError does not.
But whatever. For now, this is Good Enoughâ„¢.
1 import Model from 'ampersand-model'
2 import Sync from 'ampersand-sync'
4 import config from '../config'
5 import isObject from 'lodash.isobject'
6 import jwtDecode from 'jwt-decode'
8 export default Model.extend({
9 url: config.urlBase + '/token',
12 'Content-Type': 'application/x-www-form-urlencoded',
13 'Authorization': 'Basic ' + btoa(config.clientID + ':' + config.clientSecret),
18 access_token: 'string',
19 refresh_token: 'string',
21 token_created: 'date',
28 return !!this.access_token
31 let d = this.token_created
32 return !!this.refresh_token && (new Date() >= d.setSeconds(d.getSeconds() + this.expires_in - 900))
37 return app.profiles.get(this.profileID)
42 login (email, password) {
47 'grant_type': 'password',
51 options.success = function(resp) {
52 if (!resp.access_token) {
55 let serverAttrs = moc.parse(resp, options)
56 serverAttrs.token_created = new Date()
57 console.log(serverAttrs)
58 if (options.wait) serverAttrs = assign({}, serverAttrs)
59 if (isObject(serverAttrs) && !moc.set(serverAttrs, options)) {
62 const token = jwtDecode(moc.access_token)
63 moc.profileID = token.sub
64 moc.trigger('sync', moc, resp, options)
66 options.error = function(resp) {
67 moc.trigger('error', moc, resp, options)
69 let sync = Sync('create', moc, options)
73 // TODO: write this to chrome.storage.local
77 // TODO: clear all cached data