-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.js
162 lines (142 loc) · 3.79 KB
/
client.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
// client-side
var request = require('superagent')
module.exports = function (check_url, code_url, done) {
request.get(check_url, function (err, res) {
if (err) return done(err);
if (res.status >= 300 || res.status < 200) {
return done(res.text)
}
if (res.status == 200) {
return done(null, res.header['oauth-access-token'], res.body);
}
modal(function () {
var created = window.open(res.header['oauth-authorize-url'], 'FamilySearch Auth', 'width=400,height=600')
waitForWindow(created, function (err) {
if (err) return done(err)
var search = params(created.location.search.slice(1))
request.post(code_url)
.query({'code': search.code})
.end(function (err, res) {
if (err) return done(err)
created.close()
done(null, res.header['oauth-access-token'], res.body)
})
})
})
});
}
var Modal = React.createClass({
render: function () {
return React.DOM.div({
style: {
position: 'fixed',
backgroundColor: 'white',
top: 0,
left: 0,
right: 0,
bottom: 0,
}
},
React.DOM.button({
onClick: this.props.action,
style: {
margin: '100px auto',
backgroundColor: '#5C64FF',
border: '1px solid #ccc',
borderRadius: '10px',
fontSize: '25px',
fontWeight: 'bold',
padding: '10px 20px',
backgroundImage: 'linear-gradient(to bottom, rgb(16, 62, 247), rgb(37, 47, 115))',
color: 'rgb(234, 227, 255)',
display: 'block',
}
}, this.props.title)
)
}
})
function modal(done) {
var node = document.createElement('div')
document.body.appendChild(node)
React.renderComponent(Modal({
title: 'Login with FamilySearch',
action: function () {
node.parentNode.removeChild(node)
done()
},
}), node)
}
/*
var modal = module.exports.modal = function (check_url, next) {
module.exports(check_url, function (err, url) {
if (err) return next(err)
showDialog(url)
}, function (err, token, data) {
dialog.parentNode.removeChild(dialog)
next(err, token, data)
})
}
function showDialog(url, modal) {
var node = document.createElement('iframe');
node.src = url + '&template=mobile';
node.className = 'fsauth' + (modal ? ' modal' : '')
document.body.appendChild(node)
return node
}
*/
/**
* Expect an external window to be done sometime soon
*/
function waitForWindow(child, initial, step, done) {
if (arguments.length === 2) {
done = initial
initial = 500
step = 100
}
waitFor(initial, step, function () {
if (child.closed) {
done(new Error('User aborder auth'))
return true
}
try {
var m = child.location.search;
} catch (e) {
return false
}
if (child.location.origin !== this.location.origin) return false
done()
return true
})
}
// wait for something to happen
function waitFor(start, ival, done) {
setTimeout(function () {
if (done()) return
var clear = setInterval(function () {
if (done()) {
clearInterval(clear)
}
}, ival)
}, start)
}
var chrs = '0123456789abcdefghijklmnopqrtsuvwxyz'
function uuid(num) {
num = num || 32
var res = ''
for (var i=0; i<num; i++) {
res += chrs[parseInt(Math.random() * chrs.length)]
}
return res
}
function params(what) {
if ('string' === typeof what) return parseParams(what)
return Object.keys(what).map(function (name) {return name + '=' + encodeURIComponent(what[name])}).join('&');
}
function parseParams(what) {
var obj = {}
what.split('&').forEach(function (part) {
var subs = part.split('=')
obj[subs[0]] = decodeURIComponent(subs.slice(1).join('='))
})
return obj
}