Fixed Twitch login's handling of scope & expiration.

This commit is contained in:
Tom
2025-01-09 18:15:28 +00:00
parent 53ff28c6f7
commit c198bfb0aa

View File

@ -258,7 +258,6 @@ app.post("/api/auth/twitch/callback", async (req: any, res: any) => {
});
const body = await response.readBody();
const data = JSON.parse(body);
console.log('twitch auth data', data);
if (!data || data.message) {
console.log('Failed to validate Twitch code authentication:', data);
res.send({ authenticated: false });
@ -273,7 +272,7 @@ app.post("/api/auth/twitch/callback", async (req: any, res: any) => {
const b = await resp.readBody();
const twitch = JSON.parse(b);
if (!twitch?.data) {
console.log('Failed to fetch twitch data:', twitch?.data);
console.log('Failed to fetch twitch data:', data, twitch?.data);
res.send({ authenticated: false });
return;
}
@ -281,16 +280,15 @@ app.post("/api/auth/twitch/callback", async (req: any, res: any) => {
const account: any = await db.oneOrNone('SELECT "userId" FROM "Account" WHERE "providerAccountId" = $1', twitch.data[0].id);
if (account != null) {
const user: any = await db.one('SELECT id FROM "User" WHERE id = $1', account.userId);
console.log('User fetched successfully:', user.id);
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, { expiresIn: '30d' });
res.send({ authenticated: true, token: token });
var now = Date.now();
await db.none('UPDATE "Account" SET refresh_token = COALESCE($1, refresh_token), access_token = $2, id_token = COALESCE($3, id_token), expires_at = $4, scope = $5 WHERE "userId" = $6', [data.refresh_token, data.access_token, data.id_token, now + data.exp * 1000, data.scope, account.userId]);
const expires_at = ((now / 1000) | 0) + data.expires_in;
await db.none('UPDATE "Account" SET refresh_token = COALESCE($1, refresh_token), access_token = $2, id_token = COALESCE($3, id_token), expires_at = $4, scope = $5 WHERE "userId" = $6', [data.refresh_token, data.access_token, data.id_token, expires_at, data.scope.join(' '), account.userId]);
return;
}
res.send({ authenticated: false });
});