Move oauth credentials to own kv value
This commit is contained in:
@ -33,6 +33,13 @@ async function refreshAuth(context: RequestContext) {
|
||||
if (!currentUser || currentUser.refresh_at > Date.now())
|
||||
return await context.next();
|
||||
|
||||
const oauthData = await context.env.DATA.get(
|
||||
`oauthcredentials_${currentUser.id}`,
|
||||
{ type: "json" },
|
||||
);
|
||||
|
||||
if (!oauthData) return await context.next();
|
||||
|
||||
const refreshedTokenResponse = await fetch(
|
||||
"https://discord.com/api/v10/oauth2/token",
|
||||
{
|
||||
@ -51,7 +58,7 @@ async function refreshAuth(context: RequestContext) {
|
||||
|
||||
const accessData: { [k: string]: any } = await refreshedTokenResponse.json();
|
||||
|
||||
let userData: { [k: string]: any } = {
|
||||
let tokenData: { [k: string]: any } = {
|
||||
...accessData,
|
||||
refresh_at: Date.now() + 3600000,
|
||||
};
|
||||
@ -64,10 +71,7 @@ async function refreshAuth(context: RequestContext) {
|
||||
|
||||
if (!newDiscordData.ok) return await context.next();
|
||||
|
||||
userData = {
|
||||
...userData,
|
||||
...(await newDiscordData.json()),
|
||||
};
|
||||
const userData: { [k: string]: any } = await newDiscordData.json();
|
||||
|
||||
const updatedServerMemberReq = await fetch(
|
||||
"https://discord.com/api/v10/users/@me/guilds/242263977986359297/member",
|
||||
@ -95,6 +99,13 @@ async function refreshAuth(context: RequestContext) {
|
||||
await context.env.DATA.put(`auth_${tokenHash}`, JSON.stringify(userData), {
|
||||
expirationTtl: accessData.expires_in,
|
||||
});
|
||||
await context.env.DATA.put(
|
||||
`oauthcredentials_${userData.id}`,
|
||||
JSON.stringify(tokenData),
|
||||
{
|
||||
expirationTtl: 1209600000,
|
||||
},
|
||||
);
|
||||
|
||||
delete context.data.sid;
|
||||
|
||||
@ -348,5 +359,5 @@ export const onRequest = [
|
||||
setTheme,
|
||||
constructHTML,
|
||||
setBody,
|
||||
setHeaders,
|
||||
setHeaders
|
||||
];
|
||||
|
Reference in New Issue
Block a user