104 lines
No EOL
4.5 KiB
JavaScript
104 lines
No EOL
4.5 KiB
JavaScript
document.addEventListener('DOMContentLoaded', () => {
|
|
const loginForm = document.getElementById('login-form');
|
|
const registerForm = document.getElementById('register-form');
|
|
|
|
if (loginForm) {
|
|
loginForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const email = document.getElementById('email').value;
|
|
const password = document.getElementById('password').value;
|
|
|
|
try {
|
|
const res = await fetch('/auth/login', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ email, password })
|
|
});
|
|
const data = await res.json();
|
|
|
|
if (res.ok) {
|
|
if (data.user && window.SpotCrypto) {
|
|
try {
|
|
if (data.user.encrypted_private_key) {
|
|
await window.SpotCrypto.unlockUserPrivateKey(data.user, password);
|
|
} else {
|
|
const regData = await window.SpotCrypto.generateUserRegistrationPayload(
|
|
email,
|
|
password,
|
|
data.user.first_name || 'User',
|
|
data.user.last_name || ''
|
|
);
|
|
await fetch('/auth/init-keys', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({
|
|
public_key: regData.public_key,
|
|
encrypted_private_key: regData.encrypted_private_key,
|
|
encryption_salt: regData.encryption_salt
|
|
})
|
|
});
|
|
}
|
|
} catch (err) {
|
|
console.error('Failed to initialize or decrypt private key:', err);
|
|
}
|
|
}
|
|
window.location.href = '/'; // Redirect to map
|
|
} else {
|
|
alert(data.error || 'Login failed');
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
alert('An error occurred');
|
|
}
|
|
});
|
|
}
|
|
|
|
if (registerForm) {
|
|
registerForm.addEventListener('submit', async (e) => {
|
|
e.preventDefault();
|
|
const email = document.getElementById('email').value.trim();
|
|
const firstName = document.getElementById('first-name').value.trim();
|
|
const lastName = document.getElementById('last-name').value.trim();
|
|
const password = document.getElementById('password').value;
|
|
|
|
const submitBtn = registerForm.querySelector('button[type="submit"]');
|
|
if (submitBtn) {
|
|
submitBtn.disabled = true;
|
|
submitBtn.textContent = 'Generating Secure Keys...';
|
|
}
|
|
|
|
try {
|
|
let regPayload = { email, password, first_name: firstName, last_name: lastName };
|
|
|
|
if (window.SpotCrypto) {
|
|
regPayload = await window.SpotCrypto.generateUserRegistrationPayload(email, password, firstName, lastName);
|
|
}
|
|
|
|
const res = await fetch('/auth/register', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify(regPayload)
|
|
});
|
|
const data = await res.json();
|
|
|
|
if (res.ok) {
|
|
alert('Registration successful with client-side encryption! Please log in.');
|
|
window.location.href = '/login.html';
|
|
} else {
|
|
if (submitBtn) {
|
|
submitBtn.disabled = false;
|
|
submitBtn.textContent = 'Register';
|
|
}
|
|
alert(data.error || 'Registration failed');
|
|
}
|
|
} catch (err) {
|
|
console.error(err);
|
|
if (submitBtn) {
|
|
submitBtn.disabled = false;
|
|
submitBtn.textContent = 'Register';
|
|
}
|
|
alert('An error occurred during secure key generation or registration.');
|
|
}
|
|
});
|
|
}
|
|
}); |