-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
104 lines (90 loc) · 2.95 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
const express = require('express');
const oura = require('oura');
const moment = require('moment');
const url = require('url');
const cors = require('cors');
require('dotenv').config();
const serverUrl =
process.env.PROD_SERVER_URL || 'https://aa66-213-152-241-52.ngrok-free.app';
const ouraConfig = {
clientId: process.env.OURA_CLIENT_ID,
clientSecret: process.env.OURA_CLIENT_SECRET,
// redirectUri: `${serverUrl}/redirect`,
redirectUri: process.env.WEB_APP_REDIRECT_URL,
};
const dateFormat = 'YYYY-MM-DD';
let startLastWeek = moment().subtract(7, 'days').format(dateFormat);
let endNow = moment().format(dateFormat);
const redirect = url.parse(ouraConfig.redirectUri);
const app = express();
app.use(cors());
const port = process.env.PORT || 8000;
const host = redirect.host;
const proto = redirect.protocol;
// server paths
const promptOuraAuthAddress = proto + '//' + host + '/promptOuraAuth';
const oneWeekReadinessDataPath =
'/getReadinessData/' + startLastWeek + '/' + endNow;
app.get('/', (req, res) => {
res.send('<a href="' + promptOuraAuthAddress + '">kick off auth</a>');
});
const server = app.listen(port, () => {
console.log(`Example app listening on port ${port}`);
console.log(
`oura callback url to make sure is on the allowlist: ${ouraConfig.redirectUri}`
);
});
// save auth qr requests
const authRequests = new Map();
const authClient = oura.Auth(ouraConfig);
// prompt to begin oura authorization flow
app.get(url.parse(promptOuraAuthAddress).pathname, (req, res) => {
const state = req.query.userAddress || '69';
const authUri = authClient.token.getUri({ state });
return res
.status(200)
.set('Content-Type', 'application/json')
.send({ authUri });
});
// auth redirect from oura api
app.get(redirect.pathname, (req, res) => {
const userAddress = req.query.state || 'ERROR';
console.log(userAddress);
return authClient.code
.getToken(req.originalUrl)
.then((auth) => {
return auth.refresh().then(function (refreshed) {
const authConfig = refreshed.data;
// store user authConfig
authRequests.set(userAddress, authConfig);
});
})
.then(function () {
res.send(
'Logged into oura. Get <a href="' +
`${oneWeekReadinessDataPath}?state=${userAddress}` +
'">last week\'s readiness data</a>'
);
})
.catch(function () {
res.send('Error: oura auth failed');
});
});
// get readiness from any start to end dates
app.get('/getReadinessData/:start/:end', (req, res) => {
const state = req.query.state;
const token = req.query.access_token;
const client = new oura.Client(token);
client
.dailyReadiness(req.params.start, req.params.end)
.then(function (user) {
// res.json(JSON.stringify(user, null, 1));
return res
.status(200)
.set('Content-Type', 'application/json')
.send(JSON.stringify(user, null, 1));
})
.catch(function (error) {
console.error(error);
});
});