ducky/web

Paddy 2015-07-07 Parent:9b97c908a706

22:21f80f56cda9 Go to Latest

ducky/web/src/helpers/oauth-refresh.js

Switch to being a website instead of a Chrome app. Update our CNAME to be the more appropriate "prototype.useducky.com" when we deploy. Create a homepage and a non-onboarding page template. This mostly consisted of setting up a header component and the associated styles, and then a logged-in vs. guest flavor of said header, changing the links appropriately. We also created a simple homepage that describes what Ducky is and does, and gave a jumping-off point for it. Stubbed out a basic links page, just to get an idea for what the homepage would be like when a logged-in user navigated to the homepage (e.g., not the marketing copy). Updated our login page to _actually work_, and redirected it to the new URL for the payment setup page. Updated the payment page to actually create a subscription, and moved it from /register/payment to just /payment. Fixed a bug in our registration page that was looking for an invalid_form error when it really meant an invalid_format error. Ooops. Also, updated it to point to the new /payment endpoint instead of /register/payment. Updated our router to use the new homepage, the new links page, and updated the URL for the payment page. Updated our button styles so they should all have the right font color, padding, and border-radius, but could've potentially screwed something up. Oops. Updated our backgrounds all over to have a transparent-y white background behind the content, and a simple pattern for the rest of the body. Not sure how I feel about it just yet, but I'm not going to keep futzing with it.

History
1 import app from 'ampersand-app'
2 import xhr from 'xhr'
3 import qs from 'qs'
4 import Sync from 'ampersand-sync'
5 import config from '../config'
7 const getRefresh = (opts, callback) => {
8 const refreshOpts = {
9 url: config.urlBase + '/auth/token',
10 method: 'POST',
11 headers: {
12 'Content-Type': 'application/x-www-form-urlencoded',
13 'Authorization': 'Basic ' + btoa(config.clientID + ':' + config.clientSecret),
14 },
15 data: qs.stringify({
16 'grant_type': 'refresh_token',
17 'refresh_token': app.me.refresh_token
18 }),
19 }
20 xhr(refreshOpts, function(err, resp, body) {
21 if (resp.statusCode != 200) {
22 callback(err, resp, body)
23 return
24 }
25 if (body && resp.headers['content-type'] == 'application/json') {
26 try {
27 body = JSON.parse(body)
28 } catch (err) {
29 app.trigger('token:refreshError', body)
30 callback(err, resp, body)
31 }
32 }
33 if (body.access_token) {
34 app.me.set(body)
35 } else {
36 app.trigger('token:refreshError', body)
37 }
38 callback(err, resp, body)
39 })
40 }
42 const shouldRefreshXHR = (err, resp, body) => {
43 if(body && resp.headers['Content-Type'] == 'application/json') {
44 try {
45 body = JSON.parse(body)
46 } catch (err) {
47 return false
48 }
49 }
50 if (resp.statusCode == 401 && body.errors && body.errors[0]
51 && body.errors[0].header == 'authorization' && body.errors[0].error == 'access_denied') {
52 return true
53 }
54 return false
55 }
57 const doRefreshXHR = (opts, callback) => {
58 opts.noRefresh = true
59 getRefresh(opts, function(err, resp, body) {
60 if (body.access_token) {
61 opts.headers['Authorization'] = 'Bearer '+body.access_token
62 xhr(opts, callback)
63 }
64 })
65 }
67 const refreshXHR = (opts, callback) => {
68 return xhr(opts, function(err, resp, body) {
69 if (opts.noRefresh || !shouldRefreshXHR(err, resp, body)) {
70 callback(err, resp, body)
71 return
72 }
73 doRefreshXHR(opts, callback)
74 })
75 xhr(opts, callback)
76 }
78 const shouldRefreshSync = (resp) => {
79 if (resp && resp.errors && resp.errors[0]
80 && resp.errors[0].header == 'authorization' && resp.errors[0].error == 'access_denied') {
81 return true
82 }
83 return false
84 }
86 const doRefreshSync = (action, moc, opts) => {
87 opts.noRefresh = true
88 getRefresh(opts, function(err, resp, body) {
89 if (body.access_token) {
90 Sync(action, moc, opts)
91 }
92 })
93 }
95 const refreshSync = (action, moc, opts) => {
96 const oldError = opts.error
97 opts.error = function(resp) {
98 if(opts.noRefresh || !shouldRefreshSync(resp)) {
99 oldError(resp)
100 return
101 }
102 doRefreshSync(action, moc, opts)
103 }
104 Sync(action, moc, opts)
105 }
107 const refresh = {
108 Sync: refreshSync,
109 XHR: refreshXHR,
110 }
112 export default refresh