Skip to content

Commit 0140f28

Browse files
authored
Merge pull request #727 from PenguinMod/token_through_headers
Token through headers
2 parents 58cedfc + 6a6bdf1 commit 0140f28

9 files changed

Lines changed: 395 additions & 388 deletions

File tree

‎src/resources/authentication.js‎

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,14 @@ class Authentication {
7878
static authenticate(iframe = true) {
7979
const currentUrl = new URL(window.location.href);
8080
const redirect = currentUrl.searchParams.get("redirect");
81-
const url = `${window.location.origin}/signin?${redirect ? `redirect=${encodeURIComponent(redirect)}&`
82-
: (window.location.pathname !== "/signin" && window.location.pathname !== "/signup")
83-
? `redirect=${encodeURIComponent(currentUrl.href)}&`
84-
: ""
85-
}embed=${iframe}`;
81+
const url = `${window.location.origin}/signin?${
82+
redirect
83+
? `redirect=${encodeURIComponent(redirect)}&`
84+
: window.location.pathname !== "/signin" &&
85+
window.location.pathname !== "/signup"
86+
? `redirect=${encodeURIComponent(currentUrl.href)}&`
87+
: ""
88+
}embed=${iframe}`;
8689

8790
if (!iframe) {
8891
window.location.href = url;
@@ -122,9 +125,11 @@ class Authentication {
122125

123126
static verifyToken(username, token) {
124127
return new Promise((resolve, reject) => {
125-
fetch(
126-
`${ProjectApi.OriginApiUrl}/api/v1/users/tokenlogin?username=${username}&token=${token}`,
127-
)
128+
fetch(`${ProjectApi.OriginApiUrl}/api/v1/users/tokenlogin`, {
129+
headers: {
130+
Authorization: `Bearer ${token}`,
131+
},
132+
})
128133
.then((r) =>
129134
r
130135
.json()
@@ -153,17 +158,12 @@ class Authentication {
153158
}
154159
static logout() {
155160
return new Promise((resolve, reject) => {
156-
const username = localStorage.getItem("username");
157161
const token = localStorage.getItem("token");
158162
fetch(`${ProjectApi.OriginApiUrl}/api/v1/users/logout`, {
159163
method: "POST",
160164
headers: {
161-
"Content-Type": "application/json",
165+
Authorization: `Bearer ${token}`,
162166
},
163-
body: JSON.stringify({
164-
username: username,
165-
token: token,
166-
}),
167167
})
168168
.then((res) => {
169169
if (!res.ok) return reject("Not OK");
@@ -183,17 +183,29 @@ class Authentication {
183183
});
184184
}
185185
static async usernameFromCode(_username, token) {
186-
// name is a misnomer, was just to lazy to change after new api update.
187-
// TODO: make this less misleading
186+
// name is a misnomer, was just too lazy to change after new api update.
187+
// doesn't really matter now bc of newhome
188188
const j = await fetch(
189-
`${ProjectApi.OriginApiUrl}/api/v1/users/userfromcode?token=${token}`,
189+
`${ProjectApi.OriginApiUrl}/api/v1/users/userfromcode`,
190+
{
191+
headers: {
192+
Authorization: `Bearer ${token}`,
193+
},
194+
},
190195
).then((r) => r.json());
191196

192197
if (j.error) throw j.error;
193198

199+
// this is stupid - basically if any of these are false, we bypass the cache.
200+
// its stupid but also easy. so i won't be changing it for now.
194201
if (!j.birthdayEntered || !j.countryEntered || !j.isEmailVerified) {
195202
const extra = await fetch(
196-
`${ProjectApi.OriginApiUrl}/api/v1/users/extrainfostatus?token=${token}`,
203+
`${ProjectApi.OriginApiUrl}/api/v1/users/extrainfostatus`,
204+
{
205+
headers: {
206+
Authorization: `Bearer ${token}`,
207+
},
208+
},
197209
).then((r) => r.json());
198210

199211
j.birthdayEntered = extra.birthdayEntered;
@@ -214,10 +226,9 @@ class Authentication {
214226
method: "POST",
215227
headers: {
216228
"Content-Type": "application/json",
229+
Authorization: `Bearer ${token}`,
217230
},
218231
body: JSON.stringify({
219-
username,
220-
token,
221232
old_password: oldPassword,
222233
new_password: newPassword,
223234
}),
@@ -297,8 +308,8 @@ class Authentication {
297308
method: "POST",
298309
headers: {
299310
"Content-Type": "application/json",
311+
Authorization: `Bearer ${token}`,
300312
},
301-
body: JSON.stringify({ username, token }),
302313
},
303314
)
304315
.then((r) =>
@@ -320,8 +331,9 @@ class Authentication {
320331
method: "POST",
321332
headers: {
322333
"Content-Type": "application/json",
334+
Authorization: `Bearer ${token}`,
323335
},
324-
body: JSON.stringify({ username, token, newUsername }),
336+
body: JSON.stringify({ newUsername }),
325337
})
326338
.then((r) =>
327339
r
@@ -331,7 +343,7 @@ class Authentication {
331343

332344
// clear userfromcode cache
333345
Authentication.usernameFromCode(
334-
username,
346+
newUsername,
335347
token,
336348
true,
337349
);

0 commit comments

Comments
 (0)