add pretty console formatting

This commit is contained in:
2025-07-04 18:19:25 +02:00
parent c0fc8bccf2
commit 2fd30bd87e
13 changed files with 60 additions and 37 deletions

View File

@@ -1,6 +1,9 @@
import { RefreshingAuthProvider, exchangeCode, type AccessToken } from "@twurple/auth";
import { createAuthRecord, deleteAuthRecord, getAuthRecord, updateAuthRecord } from "./db/dbAuth";
import { logger } from ".";
import kleur from "kleur";
async function initAuth(userId: string, clientId: string, clientSecret: string, requestedIntents: string[], streamer: boolean): Promise<AccessToken> {
const port = process.env.REDIRECT_PORT ?? 3456
const redirectURL = process.env.REDIRECT_URL ?? `http://localhost:${port}`;
@@ -9,9 +12,9 @@ async function initAuth(userId: string, clientId: string, clientSecret: string,
const state = Bun.randomUUIDv7().replace(/-/g, "").slice(0, 32).toUpperCase();
// Generate random state variable to prevent cross-site-scripting attacks
const instruction = `Visit this URL as ${streamer ? 'the streamer' : 'the chatter'} to authenticate the bot.`
console.info(instruction);
console.info(`https://id.twitch.tv/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectURL}&response_type=code&scope=${requestedIntents.join('+')}&state=${state}`);
const instruction = `Visit this URL as ${kleur.red().underline().italic(streamer ? 'the streamer' : 'the chatter')} to authenticate the bot.`
logger.info(instruction);
logger.info(`https://id.twitch.tv/oauth2/authorize?client_id=${clientId}&redirect_uri=${redirectURL}&response_type=code&scope=${requestedIntents.join('+')}&state=${state}`);
const createCodePromise = () => {
let resolver: (code: string) => void;
@@ -39,20 +42,20 @@ async function initAuth(userId: string, clientId: string, clientSecret: string,
const code = await codepromise;
server.stop(false);
console.info(`Authentication code received.`);
logger.info(`Authentication code received.`);
const tokenData = await exchangeCode(clientId, clientSecret, code, redirectURL);
console.info(`Successfully authenticated code.`);
logger.info(`Successfully authenticated code.`);
await createAuthRecord(tokenData, userId);
console.info(`Successfully saved auth data in the database.`);
logger.ok(`Successfully saved auth data in the database.`);
return tokenData;
};
export async function createAuthProvider(user: string, intents: string[], streamer = false): Promise<RefreshingAuthProvider> {
const clientId = process.env.CLIENT_ID;
if (!clientId) { console.error("Please provide a CLIENT_ID in .env."); process.exit(1); };
if (!clientId) { logger.enverr("CLIENT_ID"); process.exit(1); };
const clientSecret = process.env.CLIENT_SECRET;
if (!clientSecret) { console.error("Please provide a CLIENT_SECRET in .env."); process.exit(1); };
if (!clientSecret) { logger.enverr("CLIENT_SECRET"); process.exit(1); };
const authRecord = await getAuthRecord(user, intents);
@@ -65,18 +68,18 @@ export async function createAuthProvider(user: string, intents: string[], stream
await authData.addUserForToken(token, intents);
authData.onRefresh(async (user, token) => {
console.info(`Successfully refreshed auth for user ${user}`);
logger.ok(`Successfully refreshed auth for user ${user}`);
await updateAuthRecord(user, token);
});
authData.onRefreshFailure((user, err) => {
console.error(`ERROR: Failed to refresh auth for user ${user}: ${err.name} ${err.message}`);
logger.err(`Failed to refresh auth for user ${user}: ${err.name} ${err.message}`);
});
try {
await authData.refreshAccessTokenForUser(user);
} catch (err) {
console.error(`Failed to refresh user ${user}. Please restart the bot and re-authenticate it. Make sure the user that auths the bot and the user that's defined in .env are the same.`);
logger.err(`Failed to refresh user ${user}. Please restart the bot and re-authenticate it. Make sure the user that auths the bot and the user that's defined in .env are the same.`);
await deleteAuthRecord(user);
};