Created
March 5, 2024 01:09
-
-
Save sugoidogo/45cb93a8ae01004102d219d0225f332b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<body> | |
<button id="authTwitch" hidden>Login to Twitch</button> | |
</body> | |
<script type="module"> | |
const client_id='id' | |
const client_secret='secret' | |
const redirect_uri=new URL('auth.html',location).toString() | |
const authTwitchButton=document.querySelector('button#authTwitch') | |
authTwitchButton.onclick=()=>window.open(redirect_uri) | |
import { RefreshingAuthProvider, exchangeCode } from 'https://cdn.jsdelivr.net/npm/@twurple/auth/+esm' | |
const authProvider=new RefreshingAuthProvider({ | |
client_id:client_id, | |
client_secret:client_secret | |
}) | |
import { ApiClient } from 'https://cdn.jsdelivr.net/npm/@twurple/api/+esm' | |
const twitch=new ApiClient({authProvider:authProvider}) | |
import localforage from 'https://cdn.jsdelivr.net/npm/localforage/+esm' | |
const storage=localforage.createInstance({name:location.pathname}) | |
authProvider.onRefresh((user_id,token)=>{ | |
token.user_id=user_id | |
twitchToken=token | |
storage.setItem('twitchToken',twitchToken) | |
}) | |
let twitchToken=await storage.getItem('twitchToken') | |
if(twitchToken){ | |
await authProvider.addUser(twitchToken.user_id,twitchToken,['chat']) | |
init() | |
}else{ | |
authTwitchButton.hidden=false | |
} | |
onmessage=async function(event){ | |
console.debug(event) | |
if(event.origin===location.origin){ | |
twitchToken=await exchangeCode(client_id, client_secret, event.data, redirect_uri) | |
twitchToken.user_id = await authProvider.addUserForToken(twitchToken,['chat']) | |
storage.setItem('twitchToken',twitchToken) | |
init() | |
} | |
} | |
async function init(){ | |
authTwitchButton.hidden=true | |
const rewards=await twitch.channelPoints.getCustomRewards(twitchToken.user_id,true) | |
console.debug(rewards) | |
} | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment