Skip to content

Commit

Permalink
dude
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiszh authored Sep 29, 2024
1 parent bb112ba commit 775b678
Showing 1 changed file with 41 additions and 49 deletions.
90 changes: 41 additions & 49 deletions src/twitchLogin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,62 @@ const AUTH_URL = 'https://id.twitch.tv/oauth2/authorize';

const SCOPES = 'user:write:chat user:read:follows user:read:emotes user:read:blocked_users user:manage:blocked_users';

// Function to set a cookie
function setCookie(name, value, days) {
const date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
const expires = `expires=${date.toUTCString()}`;
document.cookie = `${name}=${value};${expires};path=/`;
}

// Function to delete a cookie
function deleteCookie(name) {
document.cookie = `${name}=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/;`;
}

// Function to get a cookie value by name
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();
}

// Check if the user is logged in
const accessToken = getCookie('twitch_access_token');
const authButton = document.getElementById('topbar-button0');

// Async function to check if the user is logged in
async function handleToken() {
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
const accessToken = params.get('access_token');

if (accessToken) {
try {
setCookie('twitch_access_token', accessToken, 1);
setCookie('twitch_client_id', CLIENT_ID, 1);

const authButton = document.getElementById('topbar-button0');
authButton.textContent = 'Logout';

const userDataResponse = await fetch('https://api.twitch.tv/helix/users', {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Client-Id': CLIENT_ID
}
});

if (userDataResponse.ok) {
const userData = await userDataResponse.json();
console.log('User Data:', userData);
} else {
throw new Error('Failed to fetch user data');
}
} catch (error) {
console.error('Error processing access token:', error.message);
}
}
}

if (window.location.hash) {
handleToken();
}

async function checkLoginStatus() {
const accessToken = getCookie('twitch_access_token');
const authButton = document.getElementById('topbar-button0');
Expand All @@ -49,80 +80,41 @@ async function checkLoginStatus() {

const requiredScopes = SCOPES.split(' ');

// Check if all requiredScopes are present in the response scopes array
const hasAllScopes = requiredScopes.every(scope => data.scopes.includes(scope));

if (!hasAllScopes) {
deleteCookie('twitch_access_token');
deleteCookie('twitch_client_id');
alert('Missing some required scopes, please log in again');
window.location.reload();
authButton.textContent = 'Login with Twitch'; // Update button text
authButton.textContent = 'Login with Twitch';
} else {
authButton.textContent = 'Logout'; // Show "Logout" if logged in
authButton.textContent = 'Logout';
}
} catch (error) {
console.error('Error checking login status:', error.message);
}
} else {
authButton.textContent = 'Login with Twitch'; // Show "Login" if not logged in
authButton.textContent = 'Login with Twitch';
}
}

// Call the async function to check login status
checkLoginStatus();

// Handle button click for login/logout
authButton.addEventListener('click', async () => {
const accessToken = getCookie('twitch_access_token');
if (accessToken) {
// Logout logic
deleteCookie('twitch_access_token');
deleteCookie('twitch_client_id');
alert('Logged out successfully!');
window.location.reload();
authButton.textContent = 'Login with Twitch'; // Update button text
authButton.textContent = 'Login with Twitch';
} else {
// Login logic
const authUrl = `${AUTH_URL}?client_id=${CLIENT_ID}&redirect_uri=${REDIRECT_URI}&response_type=token&scope=${encodeURIComponent(SCOPES)}`;
window.location = authUrl; // Redirect to Twitch login
window.location = authUrl;
}
});

// Handle the token returned in the URL hash
if (window.location.hash) {
const hash = window.location.hash.substring(1);
const params = new URLSearchParams(hash);
const accessToken = params.get('access_token');

if (accessToken) {
try {
// Store the access token and client ID in cookies
setCookie('twitch_access_token', accessToken, 1); // Expires in 1 day
setCookie('twitch_client_id', CLIENT_ID, 1); // Store client ID

authButton.textContent = 'Logout'; // Update button text after successful login

// Optionally, make API requests to Twitch with the token
const userDataResponse = await fetch('https://api.twitch.tv/helix/users', {
headers: {
'Authorization': `Bearer ${accessToken}`,
'Client-Id': CLIENT_ID
}
});

if (userDataResponse.ok) {
const userData = await userDataResponse.json();
console.log('User Data:', userData);
} else {
throw new Error('Failed to fetch user data');
}
} catch (error) {
console.error('Error processing access token:', error.message);
}
}
}

// BLOCK OPERA GX USERS
const isOpera = navigator.userAgent.includes('OPR/')

Expand Down

0 comments on commit 775b678

Please sign in to comment.