From f7a2c0786285f6a89cacc22428eb834019d7a1cd Mon Sep 17 00:00:00 2001 From: Tobias Messner Date: Sat, 15 Oct 2022 23:12:39 +0200 Subject: [PATCH 1/2] Add twitch scope selector --- .../website/TwitchAuthScopeSelector.vue | 50 +++++++++++++++++++ website/src/router/default.js | 5 ++ 2 files changed, 55 insertions(+) create mode 100644 website/src/components/website/TwitchAuthScopeSelector.vue diff --git a/website/src/components/website/TwitchAuthScopeSelector.vue b/website/src/components/website/TwitchAuthScopeSelector.vue new file mode 100644 index 00000000..f239513a --- /dev/null +++ b/website/src/components/website/TwitchAuthScopeSelector.vue @@ -0,0 +1,50 @@ + + + diff --git a/website/src/router/default.js b/website/src/router/default.js index bded175b..7e6e72ef 100644 --- a/website/src/router/default.js +++ b/website/src/router/default.js @@ -12,6 +12,7 @@ import SharedRoutes from "@/router/shared-routes"; import Authenticator from "@/views/Authenticator"; import Dashboard from "@/views/Dashboard"; import ProfilePage from "@/views/ProfilePage"; +import TwitchAuthScopeSelector from "@/components/website/TwitchAuthScopeSelector"; export default [ { @@ -59,6 +60,10 @@ export default [ path: "/auth/discord/return", props: route => ({ code: route.query.code }), component: Authenticator + }, + { + path: "/twitch-scopes", + component: TwitchAuthScopeSelector } ] }, From 22297506a81b1eede834156fda092a935fa1c87e Mon Sep 17 00:00:00 2001 From: Solomon Cammack Date: Sun, 16 Oct 2022 05:07:36 +0100 Subject: [PATCH 2/2] Fix typo and clean up some other bits --- server/src/action-utils.js | 4 ++-- server/src/routes.js | 14 +++++++++----- .../components/website/TwitchAuthScopeSelector.vue | 2 +- website/src/router/default.js | 2 +- 4 files changed, 13 insertions(+), 9 deletions(-) diff --git a/server/src/action-utils.js b/server/src/action-utils.js index bac03da6..95464844 100644 --- a/server/src/action-utils.js +++ b/server/src/action-utils.js @@ -74,8 +74,8 @@ async function createRecord(Cache, tableName, records) { newRecords.forEach(record => { Cache.set(cleanID(record.id), deAirtable(record.fields), { eager: true }); }); - console.log(newRecords.length); - console.log(newRecords); + // console.log(newRecords.length); + // console.log(newRecords); } catch (e) { console.error("Airtable create failed", e); return { error: true, errorMessage: e.message }; diff --git a/server/src/routes.js b/server/src/routes.js index 710f17a4..1623514e 100644 --- a/server/src/routes.js +++ b/server/src/routes.js @@ -308,7 +308,7 @@ module.exports = ({ app, cors, Cache, io }) => { if (!TwitchEnvSet) return res.status(503).send({ error: true, message: "Twitch authentication is disabled on the server." }); let state = createState(); states[state] = req.params.scopes; - res.redirect(`https://id.twitch.tv/oauth2/authorize?client_id=${process.env.TWITCH_CLIENT_ID}&redirect_uri=${process.env.TWITCH_REDIRECT_URI}&response_type=code&scope=${req.params.scopes}&force_verify=true`); + res.redirect(`https://id.twitch.tv/oauth2/authorize?client_id=${process.env.TWITCH_CLIENT_ID}&redirect_uri=${process.env.TWITCH_REDIRECT_URI}&response_type=code&scope=${req.params.scopes}&force_verify=true&state=${state}`); }); @@ -332,14 +332,15 @@ module.exports = ({ app, cors, Cache, io }) => { if (existingChannel) { airtableResponse = await updateRecord(Cache, "Channels", existingChannel.id, { "Twitch Refresh Token": tokenInfo.accessToken, - "Twitch Scopes": tokenInfo.scopes.join(" "), + "Twitch Scopes": tokenInfo.scope.join(" "), "Channel ID": tokenInfo.userId, "Name": tokenInfo.userName }); + } else { airtableResponse = await createRecord(Cache, "Channels", [{ "Twitch Refresh Token": tokenInfo.accessToken, - "Twitch Scopes": tokenInfo.scopes.join(" "), + "Twitch Scopes": tokenInfo.scope.join(" "), "Channel ID": tokenInfo.userId, "Name": tokenInfo.userName }]); @@ -347,10 +348,13 @@ module.exports = ({ app, cors, Cache, io }) => { // console.log(airtableResponse); - return res.send("okay thanks"); + if (airtableResponse.error) { + return res.status(400).send({ error: true, errorMessage: airtableResponse.errorMessage }); + } + return res.send("poggers thanks"); } catch (e) { console.error("[Twitch Auth] error", e); - res.status(400).send({ error: true, errorMessage: e.message}); + res.status(400).send({ error: true, errorMessage: e.message }); } }); diff --git a/website/src/components/website/TwitchAuthScopeSelector.vue b/website/src/components/website/TwitchAuthScopeSelector.vue index f239513a..5344939d 100644 --- a/website/src/components/website/TwitchAuthScopeSelector.vue +++ b/website/src/components/website/TwitchAuthScopeSelector.vue @@ -32,7 +32,7 @@ export default { value: "channel:manage:predictions channel:read:predictions" }, { - text: "Title (channel:manage:broadcast)", + text: "Title/Game (channel:manage:broadcast)", value: "channel:manage:broadcast" }, { diff --git a/website/src/router/default.js b/website/src/router/default.js index 7e6e72ef..30d0b820 100644 --- a/website/src/router/default.js +++ b/website/src/router/default.js @@ -62,7 +62,7 @@ export default [ component: Authenticator }, { - path: "/twitch-scopes", + path: "/twitch-auth", component: TwitchAuthScopeSelector } ]