forked from Jarran2R/osu-nppp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.js
70 lines (62 loc) · 2.66 KB
/
auth.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
const CLIENT_ID = 'dl6k4oj0aiuyymn19opsclt6xyykbc'; // this is entirely safe for me to show, view #9 at https://dev.twitch.tv/docs/authentication/register-app/
const OAUTH_TOKEN = window.location.hash.split('access_token=')[1] ? window.location.hash.split('access_token=')[1].split('&')[0] : "";
window.history.replaceState(window.history.state, "", "/osu-nppp/");
var BOT_USER_ID; // This is the User ID of the chat bot
var CHAT_CHANNEL_USER_ID; // This is the User ID of the channel that the bot will join and listen to chat messages of
async function getAuth(token) {
// https://dev.twitch.tv/docs/authentication/validate-tokens/#how-to-validate-a-token
let response = await fetch('https://id.twitch.tv/oauth2/validate', {
method: 'GET',
headers: {
'Authorization': 'OAuth ' + OAUTH_TOKEN
}
});
if (response.status != 200) {
let data = await response.json();
document.getElementById("token").classList.add("invalid");
document.getElementById("message").textContent = "congrats i didn't even know it was possible to get this error, please refresh or something idk";
throw new Error("Token is not valid. /oauth2/validate returned status code " + response.status);
};
console.log("Validated token.");
};
async function getUserId() {
const response = await fetch(`https://api.twitch.tv/helix/users?login=${document.getElementById("username").value}`, {
method: 'GET',
headers: {
'Client-Id': CLIENT_ID,
'Authorization': 'Bearer ' + OAUTH_TOKEN
}
});
let data = await response.json();
if (response.status == 401) {
throw new Error("No OAuth token provided");
};
try {
CHAT_CHANNEL_USER_ID = data.data[0].id;
console.log("Channel ID: ", CHAT_CHANNEL_USER_ID);
} catch (error) {
document.getElementById("username").classList.add("invalid");
document.getElementById("message").textContent = "Invalid username";
throw new Error("Invalid username, " + error);
};
};
async function getBotId() {
const response = await fetch('https://api.twitch.tv/helix/users', {
method: 'GET',
headers: {
'Client-Id': CLIENT_ID,
'Authorization': 'Bearer ' + OAUTH_TOKEN
}
});
let data = await response.json();
if (response.status == 401) {
document.getElementById("message").textContent = "also didn't think this error was possible, ggs";
throw new Error("No OAuth token provided");
}
try {
BOT_USER_ID = data.data[0].id;
console.log("Bot ID: ", BOT_USER_ID);
} catch (error) {
throw new Error(error);
};
};