2025-11-12 10:13:24 -05:00
|
|
|
import { postData } from "./client.js";
|
|
|
|
|
|
|
|
|
|
const registerForm = document.getElementById("registerForm");
|
|
|
|
|
|
2025-11-28 13:54:57 -05:00
|
|
|
const error = document.querySelector(".error");
|
|
|
|
|
const errorMessage = document.querySelector(".error p");
|
|
|
|
|
const success = document.querySelector(".success");
|
|
|
|
|
|
2025-11-12 10:13:24 -05:00
|
|
|
if (registerForm) registerForm.onsubmit = async e => {
|
|
|
|
|
e.preventDefault();
|
2026-03-09 16:31:40 -04:00
|
|
|
|
2025-11-18 21:28:44 -05:00
|
|
|
if (registerForm.regPass.value !== registerForm.regVerify.value) {
|
2025-11-28 13:54:57 -05:00
|
|
|
error.style.display = "flex";
|
|
|
|
|
errorMessage.innerHTML = "Passwords must match";
|
2025-11-18 21:28:44 -05:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-09 16:31:40 -04:00
|
|
|
const salt = window.crypto.getRandomValues(new Uint8Array(16));
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const { authenticationKeyString, encryptionKeyObject } = await generateKeys(registerForm.regPass.value, salt);
|
|
|
|
|
|
|
|
|
|
console.log(authenticationKeyString);
|
|
|
|
|
console.log(encryptionKeyObject);
|
|
|
|
|
|
|
|
|
|
|
2025-11-18 21:16:35 -05:00
|
|
|
let resultObject = await postData("/register", {
|
2025-11-12 10:13:24 -05:00
|
|
|
username: registerForm.regUser.value,
|
2026-03-09 16:31:40 -04:00
|
|
|
password: authenticationKeyString,
|
|
|
|
|
role: registerForm.regRole.value,
|
|
|
|
|
salt: convertBufferToHex(salt)
|
2025-11-12 10:13:24 -05:00
|
|
|
});
|
2025-11-28 15:31:13 -05:00
|
|
|
if (resultObject.message.includes("User registered")) {
|
2025-11-28 13:54:57 -05:00
|
|
|
error.style.display = "none";
|
|
|
|
|
success.style.display = "flex";
|
|
|
|
|
} else {
|
|
|
|
|
errorMessage.innerHTML = resultObject.message;
|
|
|
|
|
error.style.display = "flex";
|
|
|
|
|
}
|
2025-11-12 10:13:24 -05:00
|
|
|
if (resultObject.success === true) {
|
2025-11-18 21:16:35 -05:00
|
|
|
window.location.href = "/login";
|
2025-11-12 10:13:24 -05:00
|
|
|
}
|
|
|
|
|
};
|
2026-03-09 16:31:40 -04:00
|
|
|
|
|
|
|
|
async function generateKeys(password, salt) {
|
|
|
|
|
const encoder = new TextEncoder();
|
|
|
|
|
|
|
|
|
|
const base = await window.crypto.subtle.importKey(
|
|
|
|
|
"raw",
|
|
|
|
|
encoder.encode(password),
|
|
|
|
|
"PBKDF2",
|
|
|
|
|
false,
|
|
|
|
|
["deriveBits", "deriveKey"]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const authSalt = new Uint8Array([...salt, ...encoder.encode("auth")]);
|
|
|
|
|
const authKeyBits = await window.crypto.subtle.deriveBits(
|
|
|
|
|
{
|
|
|
|
|
name: "PBKDF2",
|
|
|
|
|
salt: authSalt,
|
|
|
|
|
iterations: 100000,
|
|
|
|
|
hash: "SHA-256"
|
|
|
|
|
},
|
|
|
|
|
base,
|
|
|
|
|
256
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const encryptionSalt = new Uint8Array([...salt, ...encoder.encode("enc")]);
|
|
|
|
|
const encryptionKey = await window.crypto.subtle.deriveKey(
|
|
|
|
|
{
|
|
|
|
|
name: "PBKDF2",
|
|
|
|
|
salt: encryptionSalt,
|
|
|
|
|
iterations: 100000,
|
|
|
|
|
hash: "SHA-256"
|
|
|
|
|
},
|
|
|
|
|
base,
|
|
|
|
|
{ name: "AES-GCM", length: 256 },
|
|
|
|
|
false,
|
|
|
|
|
["encrypt", "decrypt"]
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
console.log(convertBufferToHex(authKeyBits));
|
|
|
|
|
console.log(encryptionKey);
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
authenticationKeyString: convertBufferToHex(authKeyBits),
|
|
|
|
|
encryptionKeyObject: encryptionKey
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function convertBufferToHex(buffer) {
|
|
|
|
|
const bytes = new Uint8Array(buffer);
|
|
|
|
|
return Array.from(bytes)
|
|
|
|
|
.map(byte => byte.toString(16).padStart(2, '0'))
|
|
|
|
|
.join('');
|
|
|
|
|
}
|
|
|
|
|
|