This repository has been archived by the owner on Oct 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fitbit-auth.html
80 lines (75 loc) · 3.06 KB
/
fitbit-auth.html
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
<link rel="import" href="./bower_components/polymer/polymer-element.html">
<dom-module id="fitbit-auth">
<script>
class FitbitAuth extends Polymer.Element {
static get is() { return 'fitbit-auth'; }
static get properties() {
return {
token: { type: String, notify: true, readonly: true, value: window.localStorage.getItem('fitbit_access_token') },
user: { type: Object, notify: true, readonly: true },
userBadges: { type: Object, notify: true },
responseType: { type: String, readonly: true, value: 'token' },
clientId: String,
redirectUri: String,
scope: { type: Array, value: FitbitAuth.defaultScope },
expiresIn: { type: Number, value: 2592000 }
};
}
static get observers() {
return [
'__getUser(token)',
'__getUserBadges(token)'
]
}
static get path() { return 'https://www.fitbit.com/oauth2/authorize?'; }
static get defaultScope() { return ['activity', 'heartrate', 'location', 'nutrition', 'profile', 'settings', 'sleep', 'social', 'weight'] }
connectedCallback() {
super.connectedCallback()
let hash = window.location.hash.toString();
if (hash.length > 0) {
this.token = this.__parseHash(hash).access_token;
window.localStorage.setItem('fitbit_access_token', this.token);
window.location = this.redirectUri;
}
}
__authorizationURL() {
try {
let query = `response_type=${this.responseType}&client_id=${this.clientId}&redirect_uri=${encodeURIComponent(this.redirectUri)}&scope=${encodeURI(this.scope.join(' '))}&expires_in=${this.expiresIn}`
return FitbitAuth.path + query;
} catch (e) {
console.error('FitbitAuth config error:', e)
return null;
}
}
__parseHash(hash) {
if (hash.length > 0) {
let stg = hash.slice(1);
let stgArray = stg.split('&');
let nestedArray = stgArray.reduce((a, c) => a.concat([c.split('=')]), []);
return nestedArray.reduce((a, c) => Object.assign({}, a, { [c[0]]: c[1] }), {});
} else {
return null;
}
}
async __getUser(token) {
if (!token) return;
let hdrs = new Headers({ 'Authorization': `Bearer ${token}` });
let response = await fetch('https://api.fitbit.com/1/user/-/profile.json', { headers: hdrs });
let user = await response.json();
user.then(json => this.user = json);
}
async __getUserBadges(token) {
if (!token) return;
let hdrs = new Headers({ 'Authorization': `Bearer ${token}` });
let response = await fetch('https://api.fitbit.com/1/user/-/badges.json', { headers: hdrs });
let badges = await response.json();
badges.then(json => this.badges = json);
}
signInWithRedirect() {
window.location = this.__authorizationURL();
}
// signInWithPopup() {}
}
window.customElements.define(FitbitAuth.is, FitbitAuth);
</script>
</dom-module>