Skip to content

Commit 0caf1cc

Browse files
Techbot121Meta Construct
authored andcommitted
I suppose this is better
1 parent 69da663 commit 0caf1cc

File tree

1 file changed

+24
-19
lines changed

1 file changed

+24
-19
lines changed

app/services/DiscordMetadata.ts

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -98,40 +98,45 @@ export class DiscordMetadata extends Service {
9898
.catch(async (err: AxiosError<OAuthErrorData>) => {
9999
const discordResponse = err.response?.data;
100100
if (discordResponse?.error === "invalid_grant") {
101-
// The provided authorization grant (e.g., authorization
102-
// code, resource owner credentials) or refresh token is
103-
// invalid, expired, revoked, does not match the redirection
104-
// URI used in the authorization request, or was issued to
105-
// another client.
101+
// The provided authorization grant or refresh token is
102+
// invalid, expired, revoked, doesn't match redirection
103+
// URI, or was issued to another client.
106104
const res = await revokeOAuthToken(data.access_token);
107105
console.warn(
108106
`[Metadata] InValID_GraNT revoking token (${res})! ${userId} [${
109107
err.code
110108
}] ${JSON.stringify(discordResponse)}`
111109
);
112-
} else
110+
} else {
113111
console.error(
114112
`[Metadata] failed fetching tokens: [${err.code}] ${JSON.stringify(
115113
discordResponse
116114
)}`
117115
);
116+
}
118117
});
119-
if (!res) return;
120118

121-
const token = res.data;
122-
await (
123-
await this.sql.getLocalDatabase()
124-
).run(
125-
"UPDATE discord_tokens SET access_token = $access_token, refresh_token = $refresh_token, expires_at = $expires_at WHERE user_id = $user_id",
126-
{
127-
$user_id: userId,
128-
$access_token: token.access_token,
129-
$refresh_token: token.refresh_token,
130-
$expires_at: Date.now() + token.expires_in * 1000,
131-
}
119+
if (res && "data" in res) {
120+
const token = res.data;
121+
const db = await this.sql.getLocalDatabase();
122+
await db.run(
123+
"UPDATE discord_tokens SET access_token = ?, refresh_token = ?, expires_at = ? WHERE user_id = ?",
124+
[
125+
token.access_token,
126+
token.refresh_token,
127+
Date.now() + token.expires_in * 1000,
128+
userId,
129+
]
130+
);
131+
return token.access_token;
132+
}
133+
console.error(
134+
`[Metadata] failed to get access token for ${userId} data: ${JSON.stringify(
135+
data
136+
)} res: ${JSON.stringify(res)}`
132137
);
133-
return token.access_token;
134138
}
139+
135140
return data.access_token;
136141
}
137142

0 commit comments

Comments
 (0)