-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrotur.js
More file actions
150 lines (115 loc) · 3.89 KB
/
Copy pathrotur.js
File metadata and controls
150 lines (115 loc) · 3.89 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
const roturState = {
is_connected: false,
authenticated: false,
user: {}
};
function randomString(length) {
let result = "";
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
const max = characters.length;
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * max));
}
return result;
}
async function login_prompt() {
if (roturState.authenticated) return "Already Logged In";
const styleUrl = "assets/roturstyle.css";
const css = await fetch(styleUrl).then(r => r.text());
const dataUri = `data:text/css;charset=utf-8,${encodeURIComponent(css)}`;
const frame = document.createElement("iframe");
frame.id = "rotur-auth";
frame.src = `https://rotur.dev/auth?system=orion&styles=${encodeURIComponent(dataUri)}`;
frame.style.visibility = "hidden";
frame.addEventListener("load", () => {
frame.style.visibility = "visible";
});
document.body.appendChild(frame);
function authHandler(event) {
if (event.origin !== "https://rotur.dev") return;
if (event.data?.type !== "rotur-auth-token") return;
frame.remove();
window.removeEventListener("message", authHandler);
roturState.userToken = event.data.token;
localStorage.setItem(
"orion-rotur",
JSON.stringify({
type: "token",
token: event.data.token
})
);
loginWithToken(event.data.token).catch(console.error);
}
window.addEventListener("message", authHandler);
return "Auth window opened";
}
async function loginWithToken(token) {
if (roturState.authenticated) return "Already Logged In";
try {
const response = await fetch(
`https://social.rotur.dev/get_user?auth=${encodeURIComponent(token)}`
);
if (!response.ok) {
throw new Error(`Authentication failed: ${response.status}`);
}
const packet = await response.json();
roturState.userToken = packet.key || token;
roturState.user = { ...packet };
initialServerLoad();
delete roturState.user.key;
delete roturState.user.password;
const friends = roturState.user["sys.friends"] || [];
const requests = roturState.user["sys.requests"] || [];
roturState.friends = {
list: friends,
requests
};
delete roturState.user["sys.friends"];
delete roturState.user["sys.requests"];
roturState.username =
roturState.designation +
"-" +
roturState.user.username +
"§" +
randomString(10);
if (roturState.my_client) {
roturState.my_client.username = roturState.username;
}
roturState.authenticated = true;
loader.hide();
return `Logged in as ${roturState.user.username}`;
} catch (error) {
roturState.authenticated = false;
say(
"<h1>Rotur is down.</h1>Looks like RoturTW's servers or their providers are down. Try again in a few minutes.",
"failed"
);
throw new Error(`Failed to login: ${error.message}`);
}
}
async function restoreRoturLogin() {
const saved = localStorage.getItem("orion-rotur");
if (!saved) return false;
try {
const data = JSON.parse(saved);
if (data.type === "token" && data.token) {
try {
let x = await loginWithToken(data.token);
return x;
} catch {
localStorage.removeItem("orion-rotur");
}
}
} catch {
localStorage.removeItem("orion-rotur");
}
return false;
}
async function initRotur() {
loader.show();
const restored = await restoreRoturLogin();
if (!restored) {
await login_prompt();
}
}
initRotur();