Fixed Twitch login's handling of scope & expiration.
This commit is contained in:
10
src/index.ts
10
src/index.ts
@@ -258,7 +258,6 @@ app.post("/api/auth/twitch/callback", async (req: any, res: any) => {
|
|||||||
});
|
});
|
||||||
const body = await response.readBody();
|
const body = await response.readBody();
|
||||||
const data = JSON.parse(body);
|
const data = JSON.parse(body);
|
||||||
console.log('twitch auth data', data);
|
|
||||||
if (!data || data.message) {
|
if (!data || data.message) {
|
||||||
console.log('Failed to validate Twitch code authentication:', data);
|
console.log('Failed to validate Twitch code authentication:', data);
|
||||||
res.send({ authenticated: false });
|
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 b = await resp.readBody();
|
||||||
const twitch = JSON.parse(b);
|
const twitch = JSON.parse(b);
|
||||||
if (!twitch?.data) {
|
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 });
|
res.send({ authenticated: false });
|
||||||
return;
|
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);
|
const account: any = await db.oneOrNone('SELECT "userId" FROM "Account" WHERE "providerAccountId" = $1', twitch.data[0].id);
|
||||||
if (account != null) {
|
if (account != null) {
|
||||||
const user: any = await db.one('SELECT id FROM "User" WHERE id = $1', account.userId);
|
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' });
|
const token = jwt.sign({ id: user.id }, process.env.JWT_SECRET, { expiresIn: '30d' });
|
||||||
res.send({ authenticated: true, token: token });
|
res.send({ authenticated: true, token: token });
|
||||||
|
|
||||||
var now = Date.now();
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
res.send({ authenticated: false });
|
res.send({ authenticated: false });
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user