Start making login functional

This commit is contained in:
X3F200C 2024-11-09 00:03:48 -05:00
parent 27887c2a72
commit f257c5de22
4 changed files with 111 additions and 2 deletions

View File

@ -45,6 +45,40 @@ function createMainWindow() {
});
}
function fetchPlutoniumManifest() {
return new Promise(function (resolve, reject) {
let request = https.request("https://cdn.plutonium.pw/updater/prod/info.json", {
"method": "GET"
}, function (response) {
if (response.statusCode !== 200) {
return reject(new Error("Failed to fetch the Plutonium launcher manifest."));
}
response.setEncoding("utf-8");
let body = "";
response.on("data", function (chunk) {
body += chunk;
});
response.on("end", function () {
try {
let data = JSON.parse(body);
resolve(data);
} catch (error) {
reject(error);
}
});
});
request.on("error", function (error) {
reject(error);
});
request.end();
});
}
electron.app.once("ready", function () {
createMainWindow();
});
@ -52,3 +86,59 @@ electron.app.once("ready", function () {
electron.app.on("window-all-closed", function () {
electron.app.quit();
});
electron.ipcMain.handle("login", function (event, username, password) {
return new Promise(function (resolve, reject) {
let payload = JSON.stringify({
"username": username,
"password": password
});
let request = https.request("https://nix.plutonium.pw/api/auth/login", {
"method": "POST",
"headers": {
"Content-Type": "application/json",
"Content-Length": payload.length,
"Authorization": "UserToken",
"X-Plutonium-Revision": String(plutoniumManifest.revision),
"User-Agent": "Nix/3.0"
}
}, function (response) {
if (response.statusCode !== 200) {
return resolve({
"status": "unauthenticated"
});
}
response.setEncoding("utf-8");
let body = "";
response.on("data", function (chunk) {
body += chunk;
});
response.on("end", function () {
try {
let data = JSON.parse(body);
if (!("token" in data)) {
reject(new Error("Login seems to be successful but no token was returned."));
}
resolve({
"status": "authenticated",
...data
});
} catch (error) {
reject(error);
}
});
});
request.on("error", function (error) {
reject(error);
});
request.write(payload);
request.end();
});
});

View File

@ -1,4 +1,12 @@
:root {
background-color: #181820;
color: #F8F8FC;
width: 100%;
height: 100%;
}
body, main {
margin: 0px;
width: 100%;
height: 100%;
}

10
src/styles/login.css Normal file
View File

@ -0,0 +1,10 @@
#login-container {
width: 256px;
height: 384px;
margin: auto;
}
#login-form {
display: flex;
flex-direction: column;
}

View File

@ -4,12 +4,13 @@
<meta charset="utf-8">
<title>Open Plutonium Launcher</title>
<link rel="stylesheet" href="../styles/common.css" />
<link rel="stylesheet" href="../styles/login.css" />
<script src="../scripts/login.js" charset="utf-8" type="text/javascript"></script>
</head>
<body>
<main>
<div>
<form>
<div id="login-container">
<form id="login-form">
<label for="username">Username :</label>
<input type="text" name="username" id="username-field" placeholder="Your Plutonium username" minLength="2" maxLength="64">
<label for="password">Password :</label>