From df46ddc0d97fedea8c9cd5804d80d41630e1120a Mon Sep 17 00:00:00 2001 From: qwerinope Date: Thu, 3 Apr 2025 10:46:18 +0200 Subject: [PATCH] add user defined REDIRECT_URI --- .example.env | 1 + compose.yml | 3 ++- src/lib/auth.ts | 7 ++++--- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/.example.env b/.example.env index 4332de1..d3b870c 100644 --- a/.example.env +++ b/.example.env @@ -1,6 +1,7 @@ CHANNEL= CLIENT_ID= CLIENT_SECRET= +REDIRECT_URI=https://qweri0p.github.io/url-params/ OAUTH_CODE= DIFFERENT_BROADCASTER=false BROADCASTER_OAUTH_CODE= diff --git a/compose.yml b/compose.yml index 8e1898b..fb8ce8d 100644 --- a/compose.yml +++ b/compose.yml @@ -29,10 +29,11 @@ services: path: ./src environment: - CHANNEL=$CHANNEL - # These env variables can be removed once the bot has sucessfully run once + # The following env variables can be removed once the bot has sucessfully run once - CLIENT_ID=$CLIENT_ID - CLIENT_SECRET=$CLIENT_SECRET - OAUTH_CODE=$OAUTH_CODE + - REDIRECT_URI=$REDIRECT_URI # This should be exactly the same as you put in the twitch dev console # If the broadcaster is different from the bot user, # the broadcaster will need to authorize the bot to perform certain actions - DIFFERENT_BROADCASTER=$DIFFERENT_BROADCASTER diff --git a/src/lib/auth.ts b/src/lib/auth.ts index cbf4f89..6a2c7b5 100644 --- a/src/lib/auth.ts +++ b/src/lib/auth.ts @@ -28,20 +28,21 @@ async function firstAccess(main = true) { const CLIENT_SECRET = process.env.CLIENT_SECRET const OAUTH_CODE = process.env.OAUTH_CODE const BROADCASTER_OAUTH_CODE = process.env.BROADCASTER_OAUTH_CODE + const REDIRECT_URI = process.env.REDIRECT_URI ?? 'https://qweri0p.github.io/url-params/' if (!CLIENT_ID) { console.error("No 'CLIENT_ID' for OAuth defined in environment variables."); process.exit(1) } if (!CLIENT_SECRET) { console.error("No 'CLIENT_SECRET' for OAuth defined in environment variables."); process.exit(1) } if ((main && !OAUTH_CODE) || (!main && !BROADCASTER_OAUTH_CODE)) { if (main) { console.error("No 'OAUTH_CODE' provided. To get the code, please visit this URL, authorize the bot and copy the 'code' from the return URL.") - console.error(`https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=http://localhost&response_type=code&scope=chat:read+chat:edit+moderator:manage:banned_users+moderation:read`) + console.error(`https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=chat:read+chat:edit+moderator:manage:banned_users+moderation:read`) } else { console.error("No 'BROADCASTER_OAUTH_CODE' provided. To get the code, please make the broadcaster visit the following URL, and get them to return the 'code' from the return URL.") - console.error(`https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=http://localhost&response_type=code&scope=moderator:manage:banned_users+moderation:read+channel:manage:moderators`) + console.error(`https://id.twitch.tv/oauth2/authorize?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=code&scope=moderator:manage:banned_users+moderation:read+channel:manage:moderators`) } process.exit(1) } - const tokens = await exchangeCode(CLIENT_ID, CLIENT_SECRET, main ? OAUTH_CODE! : BROADCASTER_OAUTH_CODE!, "http://localhost") + const tokens = await exchangeCode(CLIENT_ID, CLIENT_SECRET, main ? OAUTH_CODE! : BROADCASTER_OAUTH_CODE!, REDIRECT_URI) const auth = { CLIENT_ID, CLIENT_SECRET,