-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
173 lines (139 loc) · 3.35 KB
/
index.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
161
162
163
164
165
166
167
168
169
170
171
172
173
"use strict";
var koa = require('koa');
var session = require('koa-session');
var config = require('config');
var request = require('request');
var Strava = require('strava');
var render = require('./lib/render');
var router = require('./router');
var app = koa();
app.keys = ['some secret key thing'];
app.on('error', function (err, ctx) {
console.log();
console.log(err.stack);
console.dir(err);
console.log();
});
app
// Session Middleware
.use(session(app))
// Error Handler Middleware
.use(function* (next) {
try {
yield next;
}
catch (err) {
this.app.emit('error', err, this);
this.status = 500;
this.body = yield render('layouts/error');
}
})
// Strava Context Middleware
.use(function* (next) {
if (this.session.strava) this.state.strava = new Strava(this.session.strava);
return yield next;
})
// Router Middleware
.use(router.routes())
// Allowed Methods Middleware
.use(router.allowedMethods());
app.use(function* (next) {
var strava = this.state.strava;
if (this.path !== '/stats') return yield next;
if (strava) {
try {
let stats = yield strava.user.stats();
this.body = stats;
}
catch (err) {
throw err; // Rethrow
}
}
else {
this.redirect('/login');
}
});
app.use(function* (next) {
var strava = this.state.strava;
if (this.path !== '/followers') return yield next;
if (strava) {
try {
let followers = yield strava.user.followers();
this.body = followers;
}
catch (err) {
throw err; // Rethrow
}
}
else {
this.redirect('/login');
}
});
app.use(function* (next) {
var strava = this.state.strava;
if (this.path !== '/update') return yield next;
if (strava) {
try {
let data = yield strava.user.update({ weight: '80' });
this.body = data;
}
catch (err) {
throw err; // Rethrow
}
}
else {
this.redirect('/login');
}
});
app.use(function* (next) {
var strava = this.state.strava;
if (this.path !== '/athlete') return yield next;
if (strava) {
try {
let athlete = yield strava.athlete(580532).get();
this.body = athlete;
}
catch (err) {
throw err; // Rethrow
}
}
else {
this.redirect('/login');
}
});
app.use(function* logout(next) {
if (this.path !== '/logout') return yield next;
delete this.session.strava;
delete this.state.strava;
this.redirect('/');
});
app.use(function* login(next) {
if (this.path !== '/login') return yield next;
var options = {
client_id: config.app.id,
redirect_uri: 'http://localhost:8080/token_exchange',
scope: 'view_private,write'
};
Strava.authorize(this.res, options);
});
app.use(function* (next) {
if (this.path !== '/token_exchange') return yield next;
var code = this.query.code;
var error = this.query.error;
if (error && error === 'access_denied') {
this.redirect('/access_denied');
}
try {
let options = { client_id: config.app.id, client_secret: config.api.secret, code: code };
this.session.strava = yield Strava.getToken(options);
this.redirect('/');
}
catch (err) {
this.status = 500;
this.body = 'Unable To Retrieve User';
this.app.emit('error', err, this);
}
});
app.listen(config.app.port, function () {
console.log('Application listening on port %s', config.app.port);
});