Move oauth credentials to own kv value

This commit is contained in:
2023-10-28 03:09:34 -04:00
parent ece5baf175
commit 0bc68ef1d2
2 changed files with 27 additions and 13 deletions

View File

@ -78,7 +78,7 @@ export async function onRequestGet(context: RequestContext) {
if (tokenData.scope.search("guilds.members.read") === -1)
return jsonError("Do not touch the scopes!", 400);
let userData: { [k: string]: any } = {
const oauthData = {
...tokenData,
refresh_at: Date.now() + tokenData.expires_in * 1000 - 86400000,
};
@ -94,12 +94,7 @@ export async function onRequestGet(context: RequestContext) {
return jsonError("Failed to retrieve user", 500);
}
const apiUser: { [k: string]: any } = await userReq.json();
userData = {
...userData,
...apiUser,
};
const userData: { [k: string]: any } = await userReq.json();
const serverMemberReq = await fetch(
"https://discord.com/api/v10/users/@me/guilds/242263977986359297/member",
{
@ -138,6 +133,14 @@ export async function onRequestGet(context: RequestContext) {
expirationTtl: tokenData.expires_in,
});
await context.env.DATA.put(
`oauthcredentials_${userData.id}`,
JSON.stringify(oauthData),
{
expirationTtl: 1209600000,
},
);
return new Response(null, {
headers: {
location: stateRedirect,