oh-my-posh/website/api/refresh/index.js

46 lines
1.2 KiB
JavaScript
Raw Normal View History

2022-01-06 12:19:21 -08:00
const strava = require('../shared/strava.js');
2022-07-11 05:41:50 -07:00
const withings = require('../shared/withings.js');
2022-01-06 12:19:21 -08:00
module.exports = async function (context, req) {
2022-07-12 04:10:13 -07:00
context.log('Refresh function processed a request');
2022-01-06 12:19:21 -08:00
// strava example:
// https://ohmyposh.dev/api/refresh?segment=strava&token=<refresh_token>
try {
const refresh_token = (req.query.token || (req.body && req.body.token));
const segment = (req.query.segment || (req.body && req.body.segment));
if (!refresh_token || !segment) {
context.res = {
status: 400
};
return;
}
2022-07-12 04:10:13 -07:00
context.log(`Refreshing the ${segment} token`);
2022-01-06 12:19:21 -08:00
let body = null;
switch (segment) {
case "strava":
2022-07-11 05:41:50 -07:00
body = await strava.refreshToken(refresh_token);
break;
case "withings":
body = await withings.refreshToken(refresh_token);
2022-01-06 12:19:21 -08:00
break;
default:
2022-07-12 04:10:13 -07:00
context.log(`Unknown segment: ${segment}`);
2022-01-06 12:19:21 -08:00
context.res = {
2022-07-12 04:10:13 -07:00
body: "Unknown segment",
2022-01-06 12:19:21 -08:00
status: 400
};
return;
}
context.res.json(body);
} catch (error) {
context.log(error);
context.res = {
body: error,
status: 500
};
}
}