2023-09-07 23:36:07 +00:00
|
|
|
export async function onRequestPost({ request, env }) {
|
2023-09-08 00:00:29 +00:00
|
|
|
const origin = new URL(request.url).origin;
|
|
|
|
const { username, password, discordID } = Object.fromEntries(await request.formData().then((f) => f.entries()));
|
2023-09-07 23:36:07 +00:00
|
|
|
|
2023-09-08 00:00:29 +00:00
|
|
|
const login = await fetch("https://eactivities.union.ic.ac.uk/user/login", {
|
2023-09-07 23:36:07 +00:00
|
|
|
method: "POST",
|
|
|
|
body: JSON.stringify({ username: username, password: password, }),
|
2023-09-24 00:33:43 +00:00
|
|
|
headers: { accept: "text/html", "content-type": "application/json", },
|
|
|
|
});
|
2023-09-07 23:36:07 +00:00
|
|
|
|
2023-09-24 00:33:43 +00:00
|
|
|
if (login.ok == false) {
|
|
|
|
console.log(`😢 FAILURE ${discordID}: ${username}`);
|
2023-09-08 00:00:29 +00:00
|
|
|
return Response.redirect(`${origin}/verify/failure`, 301);
|
2023-09-07 23:36:07 +00:00
|
|
|
} else {
|
2023-09-24 00:33:43 +00:00
|
|
|
const ck = login.headers.getSetCookie().at(0).split(";").at(0);
|
|
|
|
await fetch("https://eactivities.union.ic.ac.uk/details", {
|
|
|
|
method: "GET",
|
|
|
|
headers: { accept: "text/html", cookie: ck },
|
|
|
|
});
|
|
|
|
const info = await fetch("https://eactivities.union.ic.ac.uk/ajax/863/activatetabs", {
|
|
|
|
method: "POST",
|
|
|
|
headers: { accept: "application/xml, text/xml", cookie: ck }
|
|
|
|
}).then(r => r.text());
|
|
|
|
const firstname = info.match(/alias="First Name">(.*?)<\/info/)[1];
|
|
|
|
const surname = info.match(/alias="Surname">(.*?)<\/info/)[1];
|
|
|
|
console.log(`🚀 SUCCESS ${discordID}: ${username}, ${firstname} ${surname}`);
|
|
|
|
const submit = await fetch(env.BOT_URL, {
|
|
|
|
method: "POST",
|
|
|
|
headers: { "content-type": "application/json" },
|
|
|
|
body: JSON.stringify({ id: discordID, shortcode: username, fullname: `${firstname} ${surname}`, key: env.BOT_KEY }),
|
|
|
|
});
|
|
|
|
if (submit.ok) {
|
|
|
|
return Response.redirect(`${origin}/verify/success`, 301);
|
|
|
|
} else {
|
|
|
|
return Response.redirect(`${origin}/verify/error`, 301);
|
|
|
|
}
|
2023-09-07 23:36:07 +00:00
|
|
|
}
|
|
|
|
}
|